'use strict';
var pg = require('../lib/pgClient');
const log = require('log4js').getLogger("models/picture");
const async = require('async');
let sqlStart = "START TRANSACTION;";
let sqlRollback = "ROLLBACK;";
let sqlCommit = "COMMIT;";
/**
 * 添加美物
 * @param params//添加的参数
 * @param callback
 */
//添加的sql语句
const add_sql1 = 'SELECT * FROM users WHERE id = $1';//有没有用户
const add_sql5 = 'SELECT id FROM picture WHERE name = $1';//查重名图片
const add_sql4 = 'SELECT id FROM type WHERE id = $1';//分类
const add_sql2 = 'INSERT INTO picture (name, imgurl, size, path, tid, want, own, status, recommend, pushtime, fans, username, "desc", author, point, del, height) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) RETURNING id';
const add_sql6 = 'UPDATE users SET p_wantnum = $1 WHERE id = $2';//修改用户想要作品数
const add_sql7 = 'UPDATE users SET p_ownnum = $1 WHERE id = $2';//修改用户拥有作品数
const add_sql10 = 'UPDATE users SET reqestnum = $1 WHERE id = $2';//修改用户发布作品数
const add_sql8 = 'INSERT INTO ownlogs (pid, userid, createtime, type) VALUES ($1, $2, $3, $4)';//添加拥有记录
const add_sql9 = 'INSERT INTO wantlogs (pid, userid, createtime, type) VALUES ($1, $2, $3, $4)';//添加想要记录
exports.add_picture = function(params, user, args, callback) {
    var the_user;
    const params1 = [
        user.id
    ];
    const params4 = [
        args.type
    ];
    const params5 = [
        args.name
    ];
    pg.connect(function (err, client, done) {
        client.query(sqlStart, function () {
            client.query(add_sql1, params1, function (err, result) {//查作者
                if (err) {
                    log.error(err);
                    client.query(sqlRollback, function (errRollback) {
                        if (errRollback) {
                            log.error('查找作者失败');
                        }
                        done();
                        callback(err, null);
                    })
                } else if (result.rowCount == 0) {
                    log.error('没有此用户');
                    client.query(sqlRollback, function (errRollback) {
                        if (errRollback) {
                            log.error('没有此用户');
                        }
                        done();
                        return callback(err,'null');
                    })
                } else {
                    the_user = result.rows[0];
                    const params10 = [
                        the_user.reqestnum + 1,
                        user.id
                    ];
                    client.query(add_sql5, params5, function (err, result) {//查重名
                        if (err) {
                            log.error(err);
                            client.query(sqlRollback, function (errRollback) {
                                if (errRollback) {
                                    log.error('查找作者失败');
                                }
                                done();
                                callback(err, null);
                            })
                        } else if (result.rowCount > 0) {
                            log.error('有重名作品');
                            client.query(sqlRollback, function (errRollback) {
                                if (errRollback) {
                                    log.error('有重名作品');
                                }
                                done();
                                return callback(err, 'exit_name');
                            })
                        } else {
                            client.query(add_sql4, params4, function (err, result) {//查分类
                                if (err) {
                                    log.error(err);
                                    client.query(sqlRollback, function (errRollback) {
                                        if (errRollback) {
                                            log.error('查分类失败');
                                        }
                                        done();
                                        callback(err, null);
                                    })
                                } else if (result.rowCount == 0) {
                                    log.error('没有此分类');
                                    client.query(sqlRollback, function (errRollback) {
                                        if (errRollback) {
                                            log.error('没有此分类');
                                        }
                                        done();
                                        return callback(err,'null_type');
                                    })
                                } else {
                                    client.query(add_sql2, params, function (err, result) {//添加图片
                                        if (err || result.rowCount == 0) {//失败
                                            log.error(err);
                                            client.query(sqlRollback, function (errRollback) {
                                                if (errRollback) {
                                                    log.error('添加图片失败');
                                                }
                                                done();
                                                callback(err,null);
                                            });
                                        } else {
                                            let picture = result.rows[0];
                                            const params8 = [
                                                picture.id,
                                                the_user.id,
                                                new Date(),
                                                1
                                            ];
                                            const params9 = [
                                                picture.id,
                                                the_user.id,
                                                new Date(),
                                                1
                                            ];
                                            client.query(add_sql10, params10, function (err, result) {//添加用户想要记录
                                                if (err || result.rowCount == 0) {
                                                    log.error(err);
                                                    client.query(sqlRollback, function (errRollback) {
                                                        if (errRollback) {
                                                            log.error('添加用户想要记录失败');
                                                        }
                                                        done();
                                                        callback(err, null);
                                                    });
                                                } else {

                                                        if (args.want == "1") {
                                                            client.query(add_sql6, [(the_user.p_wantnum + 1), the_user.id], function (err, result) {//修改用户想要
                                                                if (err || result.rowCount == 0) {
                                                                    log.error(err);
                                                                    client.query(sqlRollback, function (errRollback) {
                                                                        if (errRollback) {
                                                                            log.error('添加推荐失败');
                                                                        }
                                                                        done();
                                                                        callback(err, null);
                                                                    });
                                                                } else {
                                                                    client.query(add_sql9, params9, function (err, result) {//添加用户想要记录
                                                                        if (err || result.rowCount == 0) {
                                                                            log.error(err);
                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                if (errRollback) {
                                                                                    log.error('添加用户想要记录失败');
                                                                                }
                                                                                done();
                                                                                callback(err, null);
                                                                            });
                                                                        } else {
                                                                            if(args.own == "1"){
                                                                                client.query(add_sql7, [(the_user.p_ownnum + 1), the_user.id], function (err, result) {//修改用户拥有
                                                                                    if (err || result.rowCount == 0) {
                                                                                        log.error(err);
                                                                                        client.query(sqlRollback, function (errRollback) {
                                                                                            if (errRollback) {
                                                                                                log.error('修改用户拥有失败');
                                                                                            }
                                                                                            done();
                                                                                            callback(err, null);
                                                                                        });
                                                                                    } else {
                                                                                        client.query(add_sql8, params8, function (err, result) {//添加用户拥有记录
                                                                                            if (err || result.rowCount == 0) {
                                                                                                log.error(err);
                                                                                                client.query(sqlRollback, function (errRollback) {
                                                                                                    if (errRollback) {
                                                                                                        log.error('添加用户拥有记录失败');
                                                                                                    }
                                                                                                    done();
                                                                                                    callback(err, null);
                                                                                                });
                                                                                            } else {
                                                                                                client.query(sqlCommit, function (errCommit) {
                                                                                                    if (errCommit) {
                                                                                                        client.query(sqlRollback, function (errRollback) {
                                                                                                            if (errRollback) {
                                                                                                                log.error('回滚失败');
                                                                                                            }
                                                                                                            done();
                                                                                                            callback(err, null);
                                                                                                        });
                                                                                                    } else {
                                                                                                        done();
                                                                                                        return callback(err, result);//修改成功
                                                                                                    }
                                                                                                });
                                                                                            }
                                                                                        });
                                                                                    }
                                                                                });
                                                                            }else {
                                                                                client.query(sqlCommit, function (errCommit) {
                                                                                    if (errCommit) {
                                                                                        client.query(sqlRollback, function (errRollback) {
                                                                                            if (errRollback) {
                                                                                                log.error('回滚失败');
                                                                                            }
                                                                                            done();
                                                                                            callback(err, null);
                                                                                        });
                                                                                    } else {
                                                                                        done();
                                                                                        return callback(err, result);//修改成功
                                                                                    }
                                                                                });
                                                                            }
                                                                        }
                                                                    });
                                                                }
                                                            });
                                                        } else if (args.own == "1") {
                                                            client.query(add_sql7, [(the_user.p_ownnum + 1), the_user.id], function (err, result) {//修改用户拥有
                                                                if (err || result.rowCount == 0) {
                                                                    log.error(err);
                                                                    client.query(sqlRollback, function (errRollback) {
                                                                        if (errRollback) {
                                                                            log.error('修改用户拥有失败');
                                                                        }
                                                                        done();
                                                                        callback(err, null);
                                                                    });
                                                                } else {
                                                                    client.query(add_sql8, params8, function (err, result) {//添加用户拥有记录
                                                                        if (err || result.rowCount == 0) {
                                                                            log.error(err);
                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                if (errRollback) {
                                                                                    log.error('添加用户拥有记录失败');
                                                                                }
                                                                                done();
                                                                                callback(err, null);
                                                                            });
                                                                        } else {
                                                                            if(args.want == "1"){
                                                                                client.query(add_sql7, [(the_user.p_ownnum + 1), the_user.id], function (err, result) {//修改用户拥有
                                                                                    if (err || result.rowCount == 0) {
                                                                                        log.error(err);
                                                                                        client.query(sqlRollback, function (errRollback) {
                                                                                            if (errRollback) {
                                                                                                log.error('修改用户拥有失败');
                                                                                            }
                                                                                            done();
                                                                                            callback(err, null);
                                                                                        });
                                                                                    } else {
                                                                                        client.query(add_sql9, params9, function (err, result) {//添加用户拥有记录
                                                                                            if (err || result.rowCount == 0) {
                                                                                                log.error(err);
                                                                                                client.query(sqlRollback, function (errRollback) {
                                                                                                    if (errRollback) {
                                                                                                        log.error('添加用户拥有记录失败');
                                                                                                    }
                                                                                                    done();
                                                                                                    callback(err, null);
                                                                                                });
                                                                                            } else {
                                                                                                client.query(sqlCommit, function (errCommit) {
                                                                                                    if (errCommit) {
                                                                                                        client.query(sqlRollback, function (errRollback) {
                                                                                                            if (errRollback) {
                                                                                                                log.error('回滚失败');
                                                                                                            }
                                                                                                            done();
                                                                                                            callback(err, null);
                                                                                                        });
                                                                                                    } else {
                                                                                                        done();
                                                                                                        return callback(err, result);//修改成功
                                                                                                    }
                                                                                                });
                                                                                            }
                                                                                        });
                                                                                    }
                                                                                });
                                                                            }else {
                                                                                client.query(sqlCommit, function (errCommit) {
                                                                                    if (errCommit) {
                                                                                        client.query(sqlRollback, function (errRollback) {
                                                                                            if (errRollback) {
                                                                                                log.error('回滚失败');
                                                                                            }
                                                                                            done();
                                                                                            callback(err, null);
                                                                                        });
                                                                                    } else {
                                                                                        done();
                                                                                        return callback(err, result);//修改成功
                                                                                    }
                                                                                });
                                                                            }
                                                                        }
                                                                    });
                                                                }
                                                            });
                                                        } else {
                                                            client.query(sqlCommit, function (errCommit) {
                                                                if (errCommit) {
                                                                    client.query(sqlRollback, function (errRollback) {
                                                                        if (errRollback) {
                                                                            log.error('回滚失败');
                                                                        }
                                                                        done();
                                                                        callback(err, null);
                                                                    });
                                                                } else {
                                                                    done();
                                                                    return callback(err, result);//修改成功
                                                                }
                                                            });
                                                        }

                                                }
                                            })
                                        }
                                    })
                                }
                            });
                        }
                    });
                }
            });
        });
    })
};
/**
 * 后台多条件查询图片列表
 * @param params//查询条件
 * @param args//判断信息
 * @param callback
 */
exports.list_b = function(params, args, callback){
    //let sql = 'SELECT p.*, t.typename FROM picture p, type t WHERE p.tid = t.id';
    let sql = 'select t.typename, p.* from type t right join picture p on t.id = p.tid where 1=1';
    let number = 1;
    if(args.type && args.type !='all'){//添加分类条件限制
        sql += ' AND t.typename = $'+number;
        number ++;
    }
    if(args.keyword){//对图片名称、用户名、描述进行模糊查询
        sql +=' AND ( p.name like $'+number+' OR p.username like $'+number+' OR p."desc" like $'+number+' )';
        number ++;
    }
    if(args.createtime == 'new'){
        sql +=' AND p.pushtime > $'+number+' AND p.pushtime < $'+(number+1);
        number++;
        number++;
    }
    if(args.createtime == 'yesterday'){
        sql +=' AND p.pushtime > $'+number+' AND p.pushtime < $'+(number+1);
        number++;
        number++;
    }
    if(args.createtime == 'weak'){
        sql +=' AND p.pushtime > $'+number+' AND p.pushtime < $'+(number+1);
        number++;
        number++;
    }
    if(args.createtime == 'month'){
        sql +=' AND p.pushtime > $'+number+' AND p.pushtime < $'+(number+1);
        number++;
        number++;
    }
    sql +=' AND p.del = $'+number;
    number++
    // if(args.sotring){//排序字段（倒序排列）
    //     sql +=' ORDER BY $'+number+' DESC';
    //     number++;
    // }
    if(args.sotring == 'pushtime'){//发布时间（倒序排列）
        sql +=' ORDER BY p.pushtime DESC';
    }
    if(args.sotring == 'want'){//想要（倒序排列）
        sql +=' ORDER BY p.want DESC';
    }
    if(args.sotring == 'own'){//拥有（倒序排列）
        sql +=' ORDER BY p.own DESC';
    }
    sql +=' LIMIT $'+number+' OFFSET $'+(number + 1);
    pg.query(sql, params, function(err, result){
        if(err){//报错
            callback (err, null);
        }else{
            callback (err, result);//sql执行没问题
        }
    })

};
/**
 * 后台多条件查询图片列表数据统计
 * @param params//查询条件
 * @param args//判断信息
 * @param callback
 */
exports.count_b = function(params, args, callback){
    //let sql = 'SELECT p.*, t.typename FROM picture p, type t WHERE p.tid = t.id';
    let sql = 'select t.typename, p.* from type t right join picture p on t.id = p.tid where 1=1';
    let number = 1;
    if(args.type && args.type !='all'){//添加分类条件限制
        sql += ' AND t.typename = $'+number;
        number ++;
    }
    if(args.keyword){//对图片名称、用户名、描述进行模糊查询
        sql +=' AND ( p.name like $'+number+' OR p.username like $'+number+' OR p."desc" like $'+number+' )';
        number++;
    }
    if(args.keyword){//对图片名称、用户名、描述进行模糊查询
        sql +=' AND ( p.name like $'+number+' OR p.username like $'+number+' OR p."desc" like $'+number+' )';
        number ++;
    }
    if(args.createtime == 'new'){
        sql +=' AND p.pushtime > $'+number+' AND p.pushtime < $'+(number+1);
        number++;
        number++;
    }
    if(args.createtime == 'yesterday'){
        sql +=' AND p.pushtime > $'+number+' AND p.pushtime < $'+(number+1);
        number++;
        number++;
    }
    if(args.createtime == 'weak'){
        sql +=' AND p.pushtime > $'+number+' AND p.pushtime < $'+(number+1);
        number++;
        number++;
    }
    if(args.createtime == 'month'){
        sql +=' AND p.pushtime > $'+number+' AND p.pushtime < $'+(number+1);
        number++;
        number++;
    }
    sql +=' AND p.del = $'+number;
    number++
    pg.query(sql, params, function(err, result){
        if(err){//报错
            callback (err, null);
        }else{
            callback (err, result);//sql执行没问题
        }
    });
};
/**
 * 图片详情
 * @param params
 * @param callback
 */
const info_sql = 'SELECT p.id, p.name, p.imgurl, p."desc", p.want, p.own, p.point, u.username, u.headurl, p.author, p.pushtime, p.tid FROM picture p, users u WHERE p.author = u.id AND p.id = $1 AND p.status = $2 AND p.del = $3';//查询语句
const info_sql2 = 'SELECT u.headurl, u.username, w.userid FROM users u, wantlogs w WHERE u.id = w.userid AND w.pid = $1 AND w.type = $2 ORDER BY w.createtime DESC';
const info_sql3 = 'SELECT u.headurl, u.username, o.userid FROM users u, ownlogs o WHERE u.id = o.userid AND o.pid = $1 AND o.type = $2 ORDER BY o.createtime DESC';
const info_sql4 = 'SELECT * FROM wantlogs WHERE userid = $1 AND pid = $2 AND type = $3';
const info_sql5 = 'SELECT * FROM ownlogs WHERE userid = $1 AND pid = $2 AND type = $3';
exports.info = function(params, args, callback){
    let infos={};
    const params1= [
        params[0],
        1
    ];
    const params4 = [
        args.userid,
        args.id,
        1
    ];
    pg.query(info_sql, params, function(err, result){
        if(err){//报错
            callback (err, null);
        }else if(result.rowCount == 0) {
            callback (err, 'null');//没有这张图片
        }else {
            let picture = result.rows[0];
            infos.picture=picture;
            pg.query(info_sql2, params1, function(err, result){
                if(err){//报错
                    callback (err, null);
                }else {
                    let p_want = result.rows;
                    infos.p_want=p_want;
                    pg.query(info_sql3, params1, function(err, result){
                        if(err){//报错
                            callback (err, null);
                        }else {
                            let p_own = result.rows;
                            infos.p_own=p_own;
                            pg.query(info_sql4, params4, function(err, result){
                                if(err){//报错
                                    callback (err, null);
                                }else {
                                    let u_want = result.rowCount;
                                    infos.u_want=u_want;
                                    pg.query(info_sql5, params4, function(err, result){
                                        if(err){//报错
                                            callback (err, null);
                                        }else {
                                            let u_own = result.rowCount;
                                            infos.u_own=u_own;
                                            callback (err, infos);//执行结束
                                        }
                                    });
                                }
                            });
                        }
                    });
                }
            });
        }
    });
};
/**
 * 后台编辑图片
 * @param params
 * @param args
 * @param callback
 */
const edit_sqlq1 = 'SELECT * FROM picture WHERE id = $1';
exports.edit_b = function(params, args, callback){
    const params1 = [
        args.id
    ];
    //编辑图片的名称、链接、大小、路径、分类id、状态、是否推荐、品牌分类、描述
    let sql = 'UPDATE picture SET name = $1, imgurl = $2, size = $3, path = $4, tid = $5, status = $6, recommend = $7, category = $8, "desc" = $9 WHERE id = $10';
    pg.query(edit_sqlq1, params1, function(err, result){//执行查找图片操作
        if(err){//报错
            callback (err,null);
        }else  if(result.rowCount==0){
            callback (err, null);
        }else {
            pg.query(sql, params, function(err,result){//执行编辑操作
                if(err){
                    callback (err,null);
                }else{
                    callback (err, result)//执行操作结束
                }
            });
        }
    });
};
/**
 * 批量编辑图片状态
 */
exports.upd_status = function(args, callback){
    let ids = args.ids;//ids 是数组
    let theids = "'"+ids.join("','")+"'";//将数组ids整理成字符串
    const upd_sql = 'UPDATE picture SET status = $1 WHERE id IN ('+theids+')';//编辑id在theids的图片数据
    const params =[
        args.status
    ]
    pg.query(upd_sql, params, function(err,result){//执行编辑操作
        if(err){//报错
            callback (err,null);
        }else {
            callback (err,result);//执行结束
        }
    });
};
/**
 * 批量编辑图片的首页推荐
 * @param args
 * @param callback
 */
exports.upd_recommend = function(args, callback){
    let ids = args.ids;//ids 是数组
    let theids = "'"+ids.join("','")+"'";//将数组ids整理成字符串
    const sql1 = ' DELETE FROM recommend WHERE pid IN ('+theids+')';//删除推荐记录
    const sql2 = 'SELECT pid FROM recommend WHERE pid IN ('+theids+')';//查找这个图片是否正处于推荐中状态；
    const sql3 = 'INSERT INTO recommend (pid, createtime) VALUES ($1, $2)';//添加推荐中记录表；
    const sql4 = 'UPDATE picture SET recommend = $1 WHERE id IN ('+theids+')';//修改图片推荐状态
    const params4 = [
        args.recommend
    ];
    pg.connect(function (err, client, done) {
        client.query(sqlStart, function () {
            if(args.recommend == 1) {
                client.query(sql2, [], function (err, result) {//差推荐表
                    if (err) {
                        log.error(err);
                        client.query(sqlRollback, function (errRollback) {
                            if (errRollback) {
                                log.error('查找推荐失败');
                            }
                            done();
                            callback(err, null);
                        })
                    } else if (result.rowCount > 0) {
                        log.error('有的推荐正处于推荐状态');
                        client.query(sqlRollback, function (errRollback) {
                            if (errRollback) {
                                log.error('有的推荐正处于推荐状态');
                            }
                            done();
                            return callback(err, 'exit');
                        })
                    } else {
                        client.query(sql4, params4, function (err, result) {//修改图片推荐状态
                            if (err || result.rowCount == 0) {
                                a = 1;
                                log.error(err);
                                client.query(sqlRollback, function (errRollback) {
                                    if (errRollback) {
                                        log.error('修改推荐失败');
                                    }
                                    done();
                                    callback(err, null);
                                });
                            } else {
                                let ids = args.ids;
                                let len = ids.length;
                                let a = 0;
                                for (let i = 0; i < len; i++) {
                                    let params3 = [
                                        ids[i],
                                        new Date()
                                    ];
                                    client.query(sql3, params3, function (err, result) {//添加推荐
                                        if (err || result.rowCount == 0) {
                                            log.error(err);
                                            client.query(sqlRollback, function (errRollback) {
                                                if (errRollback) {
                                                    log.error('添加推荐失败');
                                                }
                                                done();
                                                callback(err, null);
                                            });
                                        } else {
                                            a++;
                                            if (a == len) {//执行结束
                                                client.query(sqlCommit, function (errCommit) {
                                                    if (errCommit) {
                                                        client.query(sqlRollback, function (errRollback) {
                                                            if (errRollback) {
                                                                log.error('回滚失败');
                                                            }
                                                            done();
                                                            callback(err, null);
                                                        });
                                                    } else {
                                                        done();
                                                        return callback(err, result);//修改成功
                                                    }

                                                });
                                            }
                                        }
                                    });
                                }

                            }
                        });
                    }
                });
            } else {//取消推荐
                client.query(sql2, [], function (err, result) {//查推荐表
                    if (err) {
                        log.error(err);
                        client.query(sqlRollback, function (errRollback) {
                            if (errRollback) {
                                log.error('查找推荐失败');
                            }
                            done();
                            callback(err, null);
                        })
                    } else if (result.rowCount == 0) {
                        log.error('您要取消的没有一条处于推荐状态');
                        client.query(sqlRollback, function (errRollback) {
                            if (errRollback) {
                                log.error('您要取消的没有一条处于推荐状态');
                            }
                            done();
                            return callback(err, null);
                        })
                    } else {
                        client.query(sql4, params4, function (err, result) {//修改图片推荐状态
                            if (err || result.rowCount == 0) {
                                a = 1;
                                log.error(err);
                                client.query(sqlRollback, function (errRollback) {
                                    if (errRollback) {
                                        log.error('修改推荐失败');
                                    }
                                    done();
                                    callback(err, null);
                                });
                            } else {
                                client.query(sql1, [], function (err, result) {//删除推荐记录
                                    if (err || result.rowCount == 0) {
                                        log.error(err);
                                        client.query(sqlRollback, function (errRollback) {
                                            if (errRollback) {
                                                log.error('删除推荐记录失败');
                                            }
                                            done();
                                            callback(err, null);
                                        });
                                    } else {
                                        client.query(sqlCommit, function (errCommit) {//提交，结束进程
                                            if (errCommit) {
                                                client.query(sqlRollback, function (errRollback) {
                                                    if (errRollback) {
                                                        log.error('回滚失败');
                                                    }
                                                    done();
                                                    callback(err, null);
                                                });
                                            } else {
                                                done();
                                                return callback(err, result);//修改成功
                                            }
                                        });
                                    }
                                });
                            }
                        });
                    }
                });
            }
        });
    });
};
/**
 * 后台批量删除照片
 * @param callback
 */
exports.remove = function(args, callback){
    let ids = args.ids;//ids 是数组
    let theids = "'"+ids.join("','")+"'";//将数组ids整理成字符串
    const sql1 = 'SELECT * FROM picture WHERE id in ('+theids+') AND del = $1';//是否有已经被删除的图片
    const sql2 = 'UPDATE picture SET del = $1 WHERE id in ('+theids+') RETURNING author, del';//删除图片
    const sql3 = 'DELETE FROM projestlogs WHERE pictureid in ('+theids+')';//删除项目关联记录
    const sql4 = 'DELETE FROM brandlogs WHERE pictureid in ('+theids+')';//删除品牌关联记录
    const sql5 = 'DELETE FROM recommend WHERE pid in ('+theids+')';//删除推荐记录
    const sql6 = 'DELETE FROM ownlogs WHERE pid in ('+theids+') AND type = $1 RETURNING userid';//删除拥有记录
    const sql7 = 'DELETE FROM wantlogs WHERE pid in ('+theids+') AND type = $1 RETURNING userid';//删除想要记录

    const params = [2];
    const params1 = [1];
    let a = 0;
    let b = 0;
    let c = 0;
    pg.query(sql1, params, function(err, result){//是否有已经被删除的图片
        if(err){
            callback (err, null);
        }else if (result.rowCount > 0){
            callback (err, 'exit_del');//存在已经被删除的图片
        }else {
            pg.connect(function (err, client, done){
                client.query(sqlStart, function(){
                   client.query(sql2, params, function(err, result){//删除图片(修改状态为删除)
                       if(err){
                           log.error('删除图片(修改状态为删除)报错');
                           client.query(sqlRollback, function(errRollback){
                               log.error('回滚');
                               if(errRollback){
                                   log.error('回滚失败');
                               }
                               done();
                               callback (err, null);
                           })
                       }else {
                           const rusers = result.rows;
                           client.query(sql3, [], function (err, result) {
                               if (err) {
                                   log.error('删除项目记录失败');
                                   client.query(sqlRollback, function (errRollback) {
                                       if (errRollback) {
                                           log.error('回滚失败');
                                       }
                                       done();
                                       callback(err, null);
                                   })
                               } else {
                                   client.query(sql4, [], function (err, result) {
                                       if (err) {
                                           log.error('删除品牌记录失败');
                                           client.query(sqlRollback, function (errRollback) {
                                               if (errRollback) {
                                                   log.error('回滚失败');
                                               }
                                               done();
                                               callback(err, null);
                                           })
                                       } else {
                                           client.query(sql5, [], function (err, result) {
                                               if (err) {
                                                   log.error('删除推荐记录失败');
                                                   client.query(sqlRollback, function (errRollback) {
                                                       if (errRollback) {
                                                           log.error('回滚失败');
                                                       }
                                                       done();
                                                       callback(err, null);
                                                   })
                                               } else {
                                                   let ruids = [];
                                                   rusers.forEach(function (one_user) {
                                                       ruids.push(one_user.author);
                                                   })
                                                   ruids = "'" + ruids.join("','") + "'";
                                                   let sql8 = 'SELECT reqestnum, id FROM users WHERE  id in (' + ruids + ')';//差发布作品的人（发布数）
                                                   let sql9 = 'UPDATE users SET reqestnum = $1 WHERE id = $2';//修改发布数
                                                   client.query(sql8, [], function (err, result) {
                                                       if (err) {
                                                           log.error('查询发布作品用户失败');
                                                           client.query(sqlRollback, function (errRollback) {
                                                               log.error('回滚');
                                                               if (errRollback) {
                                                                   log.error('回滚失败');
                                                               }
                                                               done();
                                                               callback(err, null);
                                                           })
                                                       } else {
                                                           let users = result.rows;
                                                           let len = result.rowCount;
                                                           // let a=0;
                                                           if (len > 0) {
                                                               for(let i = 0;i<len; i++){
                                                                   let params9 = [
                                                                       users[i].reqestnum > 0 ? Number(users[i].reqestnum - 1) : Number(0),
                                                                       users[i].id
                                                                   ];
                                                                   client.query(sql9, params9, function (err, result) {
                                                                       if (err || result.rowCount == 0) {
                                                                           log.error('修改用户发布作品失败');
                                                                           client.query(sqlRollback, function (errRollback) {
                                                                               if (errRollback) {
                                                                                   log.error('回滚失败');
                                                                               }
                                                                               done();
                                                                               callback(err, null);
                                                                           })
                                                                       } else {
                                                                           a++;
                                                                            if(a == len){
                                                                                client.query(sql6, params1, function(err, result){
                                                                                    if(err){
                                                                                        log.error('删除拥有记录失败');
                                                                                        client.query(sqlRollback, function (errRollback) {
                                                                                            if(errRollback){
                                                                                                log.error('回滚失败');
                                                                                            }
                                                                                            done();
                                                                                            callback (err, null);
                                                                                        })
                                                                                    }else {
                                                                                        let ousers = result.rows;
                                                                                        let ouserlen = result.rowCount;
                                                                                        let ouids = [];
                                                                                        ousers.forEach(function (one_user) {
                                                                                            ouids.push(one_user.userid);
                                                                                        })
                                                                                        ouids = "'" + ouids.join("','") + "'";
                                                                                        let sql10 = 'SELECT p_ownnum, id FROM users WHERE id in (' + ouids + ')';
                                                                                        let sql11 = 'UPDATE users SET p_ownnum = $1 WHERE id = $2';
                                                                                        if(ouserlen > 0){
                                                                                            client.query(sql10, [], function (err, result) {
                                                                                                if (err) {
                                                                                                    log.error('查询拥有作品用户失败');
                                                                                                    client.query(sqlRollback, function (errRollback) {
                                                                                                        log.error('回滚');
                                                                                                        if (errRollback) {
                                                                                                            log.error('回滚失败');
                                                                                                        }
                                                                                                        done();
                                                                                                        callback(err, null);
                                                                                                    })
                                                                                                } else {
                                                                                                    let users = result.rows;
                                                                                                    let olen = result.rowCount;
                                                                                                    if (olen > 0) {
                                                                                                        users.forEach(function (theuser) {
                                                                                                            let params11 = [
                                                                                                                theuser.p_ownnum > 0 ? (Number(theuser.p_ownnum) - 1): Number(0),
                                                                                                                theuser.id
                                                                                                            ];
                                                                                                            client.query(sql11, params11, function (err, result) {
                                                                                                                if (err || result.rowCount == 0) {
                                                                                                                    log.error('修改用户发拥有品失败');
                                                                                                                    client.query(sqlRollback, function (errRollback) {
                                                                                                                        if (errRollback) {
                                                                                                                            log.error('回滚失败');
                                                                                                                        }
                                                                                                                        done();
                                                                                                                        callback(err, null);
                                                                                                                    })
                                                                                                                } else {
                                                                                                                    b++;
                                                                                                                    if(b == olen){
                                                                                                                        client.query(sql7, params1, function(err, result){
                                                                                                                            if(err){
                                                                                                                                log.error('删除想要记录失败');
                                                                                                                                client.query(sqlRollback, function (errRollback) {
                                                                                                                                    if(errRollback){
                                                                                                                                        log.error('回滚失败');
                                                                                                                                    }
                                                                                                                                    done();
                                                                                                                                    callback (err, null);
                                                                                                                                })
                                                                                                                            }else {
                                                                                                                                let wusers = result.rows;
                                                                                                                                let wlength = result.rowCount;
                                                                                                                                let wuids = [];
                                                                                                                                wusers.forEach(function (one_user) {
                                                                                                                                    wuids.push(one_user.userid);
                                                                                                                                })
                                                                                                                                wuids = "'" + wuids.join("','") + "'";
                                                                                                                                let sql12 = 'SELECT p_wantnum, id FROM users WHERE id in (' + wuids + ')';
                                                                                                                                let sql13 = 'UPDATE users SET p_wantnum = $1 WHERE id = $2';
                                                                                                                                if(wlength > 0){
                                                                                                                                    client.query(sql12, [], function (err, result) {
                                                                                                                                        if (err) {
                                                                                                                                            log.error('查询拥有作品用户失败');
                                                                                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                                                                                log.error('回滚');
                                                                                                                                                if (errRollback) {
                                                                                                                                                    log.error('回滚失败');
                                                                                                                                                }
                                                                                                                                                done();
                                                                                                                                                callback(err, null);
                                                                                                                                            })
                                                                                                                                        } else {
                                                                                                                                            let users = result.rows;
                                                                                                                                            let wlen = result.rowCount;
                                                                                                                                            if (wlen > 0) {
                                                                                                                                                users.forEach(function (theuser) {
                                                                                                                                                    let params13 = [
                                                                                                                                                        theuser.p_wantnum > 0 ? (Number(theuser.p_wantnum) - 1): Number(0),
                                                                                                                                                        theuser.id
                                                                                                                                                    ];
                                                                                                                                                    client.query(sql13, params13, function (err, result) {
                                                                                                                                                        if (err || result.rowCount == 0) {
                                                                                                                                                            log.error('修改用户发拥有品失败');
                                                                                                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                                                                                                if (errRollback) {
                                                                                                                                                                    log.error('回滚失败');
                                                                                                                                                                }
                                                                                                                                                                done();
                                                                                                                                                                callback(err, null);
                                                                                                                                                            })
                                                                                                                                                        } else {
                                                                                                                                                            c++;
                                                                                                                                                            if(c == wlen){
                                                                                                                                                                client.query(sqlCommit, function (errCommit) {
                                                                                                                                                                    if (errCommit) {
                                                                                                                                                                        client.query(sqlRollback, function (errRollback) {
                                                                                                                                                                            if (errRollback) {
                                                                                                                                                                                log.error('回滚失败');
                                                                                                                                                                            }
                                                                                                                                                                            done();
                                                                                                                                                                            callback(err, null);
                                                                                                                                                                        });
                                                                                                                                                                    } else {
                                                                                                                                                                        done();
                                                                                                                                                                        return callback(err, result);//删除成功
                                                                                                                                                                    }
                                                                                                                                                                });
                                                                                                                                                            }
                                                                                                                                                        }
                                                                                                                                                    })
                                                                                                                                                })

                                                                                                                                            }
                                                                                                                                            if (wlen == 0) {
                                                                                                                                                client.query(sqlCommit, function (errCommit) {
                                                                                                                                                    if (errCommit) {
                                                                                                                                                        client.query(sqlRollback, function (errRollback) {
                                                                                                                                                            if (errRollback) {
                                                                                                                                                                log.error('回滚失败');
                                                                                                                                                            }
                                                                                                                                                            done();
                                                                                                                                                            callback(err, null);
                                                                                                                                                        });
                                                                                                                                                    } else {
                                                                                                                                                        done();
                                                                                                                                                        return callback(err, result);//删除成功
                                                                                                                                                    }
                                                                                                                                                });
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    })
                                                                                                                                }else {
                                                                                                                                    client.query(sqlCommit, function (errCommit) {
                                                                                                                                        if (errCommit) {
                                                                                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                                                                                if (errRollback) {
                                                                                                                                                    log.error('回滚失败');
                                                                                                                                                }
                                                                                                                                                done();
                                                                                                                                                callback(err, null);
                                                                                                                                            });
                                                                                                                                        } else {
                                                                                                                                            done();
                                                                                                                                            return callback(err, result);//删除成功
                                                                                                                                        }
                                                                                                                                    });
                                                                                                                                }

                                                                                                                            }
                                                                                                                        })
                                                                                                                    }
                                                                                                                }
                                                                                                            })
                                                                                                        })

                                                                                                    }
                                                                                                    if (olen == 0) {
                                                                                                        client.query(sql7, params1, function(err, result){
                                                                                                            if(err){
                                                                                                                log.error('删除想要记录失败');
                                                                                                                client.query(sqlRollback, function (errRollback) {
                                                                                                                    if(errRollback){
                                                                                                                        log.error('回滚失败');
                                                                                                                    }
                                                                                                                    done();
                                                                                                                    callback (err, null);
                                                                                                                })
                                                                                                            }else {
                                                                                                                let wusers = result.rows;
                                                                                                                let wlength = result.rowCount;
                                                                                                                let wuids = [];
                                                                                                                wusers.forEach(function (one_user) {
                                                                                                                    wuids.push(one_user.userid);
                                                                                                                })
                                                                                                                wuids = "'" + wuids.join("','") + "'";
                                                                                                                let sql12 = 'SELECT p_wantnum, id FROM users WHERE id in (' + wuids + ')';
                                                                                                                let sql13 = 'UPDATE users SET p_wantnum = $1 WHERE id = $2';
                                                                                                                if(wlength > 0){
                                                                                                                    client.query(sql12, [], function (err, result) {
                                                                                                                        if (err) {
                                                                                                                            log.error('查询拥有作品用户失败');
                                                                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                                                                log.error('回滚');
                                                                                                                                if (errRollback) {
                                                                                                                                    log.error('回滚失败');
                                                                                                                                }
                                                                                                                                done();
                                                                                                                                callback(err, null);
                                                                                                                            })
                                                                                                                        } else {
                                                                                                                            let users = result.rows;
                                                                                                                            let wlen = result.rowCount;
                                                                                                                            if (wlen > 0) {
                                                                                                                                users.forEach(function (theuser) {
                                                                                                                                    let params13 = [
                                                                                                                                        theuser.p_wantnum > 0 ? (Number(theuser.p_wantnum) - 1): Number(0),
                                                                                                                                        theuser.id
                                                                                                                                    ];
                                                                                                                                    client.query(sql13, params13, function (err, result) {
                                                                                                                                        if (err || result.rowCount == 0) {
                                                                                                                                            log.error('修改用户发拥有品失败');
                                                                                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                                                                                if (errRollback) {
                                                                                                                                                    log.error('回滚失败');
                                                                                                                                                }
                                                                                                                                                done();
                                                                                                                                                callback(err, null);
                                                                                                                                            })
                                                                                                                                        } else {
                                                                                                                                            c++;
                                                                                                                                            if(c == wlen){
                                                                                                                                                client.query(sqlCommit, function (errCommit) {
                                                                                                                                                    if (errCommit) {
                                                                                                                                                        client.query(sqlRollback, function (errRollback) {
                                                                                                                                                            if (errRollback) {
                                                                                                                                                                log.error('回滚失败');
                                                                                                                                                            }
                                                                                                                                                            done();
                                                                                                                                                            callback(err, null);
                                                                                                                                                        });
                                                                                                                                                    } else {
                                                                                                                                                        done();
                                                                                                                                                        return callback(err, result);//删除成功
                                                                                                                                                    }
                                                                                                                                                });
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    })
                                                                                                                                })

                                                                                                                            }
                                                                                                                            if (wlen == 0) {
                                                                                                                                client.query(sqlCommit, function (errCommit) {
                                                                                                                                    if (errCommit) {
                                                                                                                                        client.query(sqlRollback, function (errRollback) {
                                                                                                                                            if (errRollback) {
                                                                                                                                                log.error('回滚失败');
                                                                                                                                            }
                                                                                                                                            done();
                                                                                                                                            callback(err, null);
                                                                                                                                        });
                                                                                                                                    } else {
                                                                                                                                        done();
                                                                                                                                        return callback(err, result);//删除成功
                                                                                                                                    }
                                                                                                                                });
                                                                                                                            }
                                                                                                                        }
                                                                                                                    })
                                                                                                                }else {
                                                                                                                    client.query(sqlCommit, function (errCommit) {
                                                                                                                        if (errCommit) {
                                                                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                                                                if (errRollback) {
                                                                                                                                    log.error('回滚失败');
                                                                                                                                }
                                                                                                                                done();
                                                                                                                                callback(err, null);
                                                                                                                            });
                                                                                                                        } else {
                                                                                                                            done();
                                                                                                                            return callback(err, result);//删除成功
                                                                                                                        }
                                                                                                                    });
                                                                                                                }

                                                                                                            }
                                                                                                        })
                                                                                                    }
                                                                                                }
                                                                                            })
                                                                                        }else {
                                                                                            client.query(sql7, params1, function(err, result){
                                                                                                if(err){
                                                                                                    log.error('删除想要记录失败');
                                                                                                    client.query(sqlRollback, function (errRollback) {
                                                                                                        if(errRollback){
                                                                                                            log.error('回滚失败');
                                                                                                        }
                                                                                                        done();
                                                                                                        callback (err, null);
                                                                                                    })
                                                                                                }else {
                                                                                                    let wusers = result.rows;
                                                                                                    let wlength = result.rowCount;
                                                                                                    let wuids = [];
                                                                                                    wusers.forEach(function (one_user) {
                                                                                                        wuids.push(one_user.userid);
                                                                                                    })
                                                                                                    wuids = "'" + wuids.join("','") + "'";
                                                                                                    let sql12 = 'SELECT p_wantnum, id FROM users WHERE id in (' + wuids + ')';
                                                                                                    let sql13 = 'UPDATE users SET p_wantnum = $1 WHERE id = $2';
                                                                                                    if(wlength > 0){
                                                                                                        client.query(sql12, [], function (err, result) {
                                                                                                            if (err) {
                                                                                                                log.error('查询拥有作品用户失败');
                                                                                                                client.query(sqlRollback, function (errRollback) {
                                                                                                                    log.error('回滚');
                                                                                                                    if (errRollback) {
                                                                                                                        log.error('回滚失败');
                                                                                                                    }
                                                                                                                    done();
                                                                                                                    callback(err, null);
                                                                                                                })
                                                                                                            } else {
                                                                                                                let users = result.rows;
                                                                                                                let wlen = result.rowCount;
                                                                                                                if (wlen > 0) {
                                                                                                                    users.forEach(function (theuser) {
                                                                                                                        let params13 = [
                                                                                                                            theuser.p_wantnum > 0 ? (Number(theuser.p_wantnum) - 1): Number(0),
                                                                                                                            theuser.id
                                                                                                                        ];
                                                                                                                        client.query(sql13, params13, function (err, result) {
                                                                                                                            if (err || result.rowCount == 0) {
                                                                                                                                log.error('修改用户发拥有品失败');
                                                                                                                                client.query(sqlRollback, function (errRollback) {
                                                                                                                                    if (errRollback) {
                                                                                                                                        log.error('回滚失败');
                                                                                                                                    }
                                                                                                                                    done();
                                                                                                                                    callback(err, null);
                                                                                                                                })
                                                                                                                            } else {
                                                                                                                                c++;
                                                                                                                                if(c == wlen){
                                                                                                                                    client.query(sqlCommit, function (errCommit) {
                                                                                                                                        if (errCommit) {
                                                                                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                                                                                if (errRollback) {
                                                                                                                                                    log.error('回滚失败');
                                                                                                                                                }
                                                                                                                                                done();
                                                                                                                                                callback(err, null);
                                                                                                                                            });
                                                                                                                                        } else {
                                                                                                                                            done();
                                                                                                                                            return callback(err, result);//删除成功
                                                                                                                                        }
                                                                                                                                    });
                                                                                                                                }
                                                                                                                            }
                                                                                                                        })
                                                                                                                    })

                                                                                                                }
                                                                                                                if (wlen == 0) {
                                                                                                                    client.query(sqlCommit, function (errCommit) {
                                                                                                                        if (errCommit) {
                                                                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                                                                if (errRollback) {
                                                                                                                                    log.error('回滚失败');
                                                                                                                                }
                                                                                                                                done();
                                                                                                                                callback(err, null);
                                                                                                                            });
                                                                                                                        } else {
                                                                                                                            done();
                                                                                                                            return callback(err, result);//删除成功
                                                                                                                        }
                                                                                                                    });
                                                                                                                }
                                                                                                            }
                                                                                                        })
                                                                                                    }else {
                                                                                                        client.query(sqlCommit, function (errCommit) {
                                                                                                            if (errCommit) {
                                                                                                                client.query(sqlRollback, function (errRollback) {
                                                                                                                    if (errRollback) {
                                                                                                                        log.error('回滚失败');
                                                                                                                    }
                                                                                                                    done();
                                                                                                                    callback(err, null);
                                                                                                                });
                                                                                                            } else {
                                                                                                                done();
                                                                                                                return callback(err, result);//删除成功
                                                                                                            }
                                                                                                        });
                                                                                                    }

                                                                                                }
                                                                                            })
                                                                                        }

                                                                                    }
                                                                                })
                                                                            }
                                                                       }
                                                                   })
                                                               }
                                                               // users.forEach(function (theuser) {
                                                               //     let params9 = [
                                                               //         theuser.id,
                                                               //         theuser.reqestnum > 0 ? Number(theuser.reqestnum - 1) : Number(0)
                                                               //     ];
                                                               //     client.query(sql9, params9, function (err, result) {
                                                               //         if (err || result.rowCount == 0) {
                                                               //             log.error('修改用户发布作品失败');
                                                               //             client.query(sqlRollback, function (errRollback) {
                                                               //                 if (errRollback) {
                                                               //                     log.error('回滚失败');
                                                               //                 }
                                                               //                 done();
                                                               //                 callback(err, null);
                                                               //             })
                                                               //         } else {
                                                               //             a = a + 1;
                                                               //         }
                                                               //     })
                                                               // })
                                                           }
                                                           if (len == 0) {
                                                               client.query(sql6, params1, function(err, result){
                                                                   if(err){
                                                                       log.error('删除拥有记录失败');
                                                                       client.query(sqlRollback, function (errRollback) {
                                                                           if(errRollback){
                                                                               log.error('回滚失败');
                                                                           }
                                                                           done();
                                                                           callback (err, null);
                                                                       })
                                                                   }else {
                                                                       let ousers = result.rows;
                                                                       let ouserlen = result.rowCount;
                                                                       let ouids = [];
                                                                       ousers.forEach(function (one_user) {
                                                                           ouids.push(one_user.userid);
                                                                       })
                                                                       ouids = "'" + ouids.join("','") + "'";
                                                                       let sql10 = 'SELECT p_ownnum, id FROM users WHERE id in (' + ouids + ')';
                                                                       let sql11 = 'UPDATE users SET p_ownnum = $1 WHERE id = $2';
                                                                       if(ouserlen > 0){
                                                                           client.query(sql10, [], function (err, result) {
                                                                               if (err) {
                                                                                   log.error('查询拥有作品用户失败');
                                                                                   client.query(sqlRollback, function (errRollback) {
                                                                                       log.error('回滚');
                                                                                       if (errRollback) {
                                                                                           log.error('回滚失败');
                                                                                       }
                                                                                       done();
                                                                                       callback(err, null);
                                                                                   })
                                                                               } else {
                                                                                   let users = result.rows;
                                                                                   let olen = result.rowCount;
                                                                                   if (olen > 0) {
                                                                                       users.forEach(function (theuser) {
                                                                                           let params11 = [
                                                                                               theuser.p_ownnum > 0 ? (Number(theuser.p_ownnum) - 1): Number(0),
                                                                                               theuser.id
                                                                                           ];
                                                                                           client.query(sql11, params11, function (err, result) {
                                                                                               if (err || result.rowCount == 0) {
                                                                                                   log.error('修改用户发拥有品失败');
                                                                                                   client.query(sqlRollback, function (errRollback) {
                                                                                                       if (errRollback) {
                                                                                                           log.error('回滚失败');
                                                                                                       }
                                                                                                       done();
                                                                                                       callback(err, null);
                                                                                                   })
                                                                                               } else {
                                                                                                   b++;
                                                                                                   if(b == olen){
                                                                                                       client.query(sql7, params1, function(err, result){
                                                                                                           if(err){
                                                                                                               log.error('删除想要记录失败');
                                                                                                               client.query(sqlRollback, function (errRollback) {
                                                                                                                   if(errRollback){
                                                                                                                       log.error('回滚失败');
                                                                                                                   }
                                                                                                                   done();
                                                                                                                   callback (err, null);
                                                                                                               })
                                                                                                           }else {
                                                                                                               let wusers = result.rows;
                                                                                                               let wlength = result.rowCount;
                                                                                                               let wuids = [];
                                                                                                               wusers.forEach(function (one_user) {
                                                                                                                   wuids.push(one_user.userid);
                                                                                                               })
                                                                                                               wuids = "'" + wuids.join("','") + "'";
                                                                                                               let sql12 = 'SELECT p_wantnum, id FROM users WHERE id in (' + wuids + ')';
                                                                                                               let sql13 = 'UPDATE users SET p_wantnum = $1 WHERE id = $2';
                                                                                                               if(wlength > 0){
                                                                                                                   client.query(sql12, [], function (err, result) {
                                                                                                                       if (err) {
                                                                                                                           log.error('查询拥有作品用户失败');
                                                                                                                           client.query(sqlRollback, function (errRollback) {
                                                                                                                               log.error('回滚');
                                                                                                                               if (errRollback) {
                                                                                                                                   log.error('回滚失败');
                                                                                                                               }
                                                                                                                               done();
                                                                                                                               callback(err, null);
                                                                                                                           })
                                                                                                                       } else {
                                                                                                                           let users = result.rows;
                                                                                                                           let wlen = result.rowCount;
                                                                                                                           if (wlen > 0) {
                                                                                                                               users.forEach(function (theuser) {
                                                                                                                                   let params13 = [
                                                                                                                                       theuser.p_wantnum > 0 ? (Number(theuser.p_wantnum) - 1): Number(0),
                                                                                                                                       theuser.id
                                                                                                                                   ];
                                                                                                                                   client.query(sql13, params13, function (err, result) {
                                                                                                                                       if (err || result.rowCount == 0) {
                                                                                                                                           log.error('修改用户发拥有品失败');
                                                                                                                                           client.query(sqlRollback, function (errRollback) {
                                                                                                                                               if (errRollback) {
                                                                                                                                                   log.error('回滚失败');
                                                                                                                                               }
                                                                                                                                               done();
                                                                                                                                               callback(err, null);
                                                                                                                                           })
                                                                                                                                       } else {
                                                                                                                                           c++;
                                                                                                                                           if(c == wlen){
                                                                                                                                               client.query(sqlCommit, function (errCommit) {
                                                                                                                                                   if (errCommit) {
                                                                                                                                                       client.query(sqlRollback, function (errRollback) {
                                                                                                                                                           if (errRollback) {
                                                                                                                                                               log.error('回滚失败');
                                                                                                                                                           }
                                                                                                                                                           done();
                                                                                                                                                           callback(err, null);
                                                                                                                                                       });
                                                                                                                                                   } else {
                                                                                                                                                       done();
                                                                                                                                                       return callback(err, result);//删除成功
                                                                                                                                                   }
                                                                                                                                               });
                                                                                                                                           }
                                                                                                                                       }
                                                                                                                                   })
                                                                                                                               })

                                                                                                                           }
                                                                                                                           if (wlen == 0) {
                                                                                                                               client.query(sqlCommit, function (errCommit) {
                                                                                                                                   if (errCommit) {
                                                                                                                                       client.query(sqlRollback, function (errRollback) {
                                                                                                                                           if (errRollback) {
                                                                                                                                               log.error('回滚失败');
                                                                                                                                           }
                                                                                                                                           done();
                                                                                                                                           callback(err, null);
                                                                                                                                       });
                                                                                                                                   } else {
                                                                                                                                       done();
                                                                                                                                       return callback(err, result);//删除成功
                                                                                                                                   }
                                                                                                                               });
                                                                                                                           }
                                                                                                                       }
                                                                                                                   })
                                                                                                               }else {
                                                                                                                   client.query(sqlCommit, function (errCommit) {
                                                                                                                       if (errCommit) {
                                                                                                                           client.query(sqlRollback, function (errRollback) {
                                                                                                                               if (errRollback) {
                                                                                                                                   log.error('回滚失败');
                                                                                                                               }
                                                                                                                               done();
                                                                                                                               callback(err, null);
                                                                                                                           });
                                                                                                                       } else {
                                                                                                                           done();
                                                                                                                           return callback(err, result);//删除成功
                                                                                                                       }
                                                                                                                   });
                                                                                                               }

                                                                                                           }
                                                                                                       })
                                                                                                   }
                                                                                               }
                                                                                           })
                                                                                       })

                                                                                   }
                                                                                   if (olen == 0) {
                                                                                       client.query(sql7, params1, function(err, result){
                                                                                           if(err){
                                                                                               log.error('删除想要记录失败');
                                                                                               client.query(sqlRollback, function (errRollback) {
                                                                                                   if(errRollback){
                                                                                                       log.error('回滚失败');
                                                                                                   }
                                                                                                   done();
                                                                                                   callback (err, null);
                                                                                               })
                                                                                           }else {
                                                                                               let wusers = result.rows;
                                                                                               let wlength = result.rowCount;
                                                                                               let wuids = [];
                                                                                               wusers.forEach(function (one_user) {
                                                                                                   wuids.push(one_user.userid);
                                                                                               })
                                                                                               wuids = "'" + wuids.join("','") + "'";
                                                                                               let sql12 = 'SELECT p_wantnum, id FROM users WHERE id in (' + wuids + ')';
                                                                                               let sql13 = 'UPDATE users SET p_wantnum = $1 WHERE id = $2';
                                                                                               if(wlength > 0){
                                                                                                   client.query(sql12, [], function (err, result) {
                                                                                                       if (err) {
                                                                                                           log.error('查询拥有作品用户失败');
                                                                                                           client.query(sqlRollback, function (errRollback) {
                                                                                                               log.error('回滚');
                                                                                                               if (errRollback) {
                                                                                                                   log.error('回滚失败');
                                                                                                               }
                                                                                                               done();
                                                                                                               callback(err, null);
                                                                                                           })
                                                                                                       } else {
                                                                                                           let users = result.rows;
                                                                                                           let wlen = result.rowCount;
                                                                                                           if (wlen > 0) {
                                                                                                               users.forEach(function (theuser) {
                                                                                                                   let params13 = [
                                                                                                                       theuser.p_wantnum > 0 ? (Number(theuser.p_wantnum) - 1): Number(0),
                                                                                                                       theuser.id
                                                                                                                   ];
                                                                                                                   client.query(sql13, params13, function (err, result) {
                                                                                                                       if (err || result.rowCount == 0) {
                                                                                                                           log.error('修改用户发拥有品失败');
                                                                                                                           client.query(sqlRollback, function (errRollback) {
                                                                                                                               if (errRollback) {
                                                                                                                                   log.error('回滚失败');
                                                                                                                               }
                                                                                                                               done();
                                                                                                                               callback(err, null);
                                                                                                                           })
                                                                                                                       } else {
                                                                                                                           c++;
                                                                                                                           if(c == wlen){
                                                                                                                               client.query(sqlCommit, function (errCommit) {
                                                                                                                                   if (errCommit) {
                                                                                                                                       client.query(sqlRollback, function (errRollback) {
                                                                                                                                           if (errRollback) {
                                                                                                                                               log.error('回滚失败');
                                                                                                                                           }
                                                                                                                                           done();
                                                                                                                                           callback(err, null);
                                                                                                                                       });
                                                                                                                                   } else {
                                                                                                                                       done();
                                                                                                                                       return callback(err, result);//删除成功
                                                                                                                                   }
                                                                                                                               });
                                                                                                                           }
                                                                                                                       }
                                                                                                                   })
                                                                                                               })

                                                                                                           }
                                                                                                           if (wlen == 0) {
                                                                                                               client.query(sqlCommit, function (errCommit) {
                                                                                                                   if (errCommit) {
                                                                                                                       client.query(sqlRollback, function (errRollback) {
                                                                                                                           if (errRollback) {
                                                                                                                               log.error('回滚失败');
                                                                                                                           }
                                                                                                                           done();
                                                                                                                           callback(err, null);
                                                                                                                       });
                                                                                                                   } else {
                                                                                                                       done();
                                                                                                                       return callback(err, result);//删除成功
                                                                                                                   }
                                                                                                               });
                                                                                                           }
                                                                                                       }
                                                                                                   })
                                                                                               }else {
                                                                                                   client.query(sqlCommit, function (errCommit) {
                                                                                                       if (errCommit) {
                                                                                                           client.query(sqlRollback, function (errRollback) {
                                                                                                               if (errRollback) {
                                                                                                                   log.error('回滚失败');
                                                                                                               }
                                                                                                               done();
                                                                                                               callback(err, null);
                                                                                                           });
                                                                                                       } else {
                                                                                                           done();
                                                                                                           return callback(err, result);//删除成功
                                                                                                       }
                                                                                                   });
                                                                                               }

                                                                                           }
                                                                                       })
                                                                                   }
                                                                               }
                                                                           })
                                                                       }else {
                                                                           client.query(sql7, params1, function(err, result){
                                                                               if(err){
                                                                                   log.error('删除想要记录失败');
                                                                                   client.query(sqlRollback, function (errRollback) {
                                                                                       if(errRollback){
                                                                                           log.error('回滚失败');
                                                                                       }
                                                                                       done();
                                                                                       callback (err, null);
                                                                                   })
                                                                               }else {
                                                                                   let wusers = result.rows;
                                                                                   let wlength = result.rowCount;
                                                                                   let wuids = [];
                                                                                   wusers.forEach(function (one_user) {
                                                                                       wuids.push(one_user.userid);
                                                                                   })
                                                                                   wuids = "'" + wuids.join("','") + "'";
                                                                                   let sql12 = 'SELECT p_wantnum, id FROM users WHERE id in (' + wuids + ')';
                                                                                   let sql13 = 'UPDATE users SET p_wantnum = $1 WHERE id = $2';
                                                                                   if(wlength > 0){
                                                                                       client.query(sql12, [], function (err, result) {
                                                                                           if (err) {
                                                                                               log.error('查询拥有作品用户失败');
                                                                                               client.query(sqlRollback, function (errRollback) {
                                                                                                   log.error('回滚');
                                                                                                   if (errRollback) {
                                                                                                       log.error('回滚失败');
                                                                                                   }
                                                                                                   done();
                                                                                                   callback(err, null);
                                                                                               })
                                                                                           } else {
                                                                                               let users = result.rows;
                                                                                               let wlen = result.rowCount;
                                                                                               if (wlen > 0) {
                                                                                                   users.forEach(function (theuser) {
                                                                                                       let params13 = [
                                                                                                           theuser.p_wantnum > 0 ? (Number(theuser.p_wantnum) - 1): Number(0),
                                                                                                           theuser.id
                                                                                                       ];
                                                                                                       client.query(sql13, params13, function (err, result) {
                                                                                                           if (err || result.rowCount == 0) {
                                                                                                               log.error('修改用户发拥有品失败');
                                                                                                               client.query(sqlRollback, function (errRollback) {
                                                                                                                   if (errRollback) {
                                                                                                                       log.error('回滚失败');
                                                                                                                   }
                                                                                                                   done();
                                                                                                                   callback(err, null);
                                                                                                               })
                                                                                                           } else {
                                                                                                               c++;
                                                                                                               if(c == wlen){
                                                                                                                   client.query(sqlCommit, function (errCommit) {
                                                                                                                       if (errCommit) {
                                                                                                                           client.query(sqlRollback, function (errRollback) {
                                                                                                                               if (errRollback) {
                                                                                                                                   log.error('回滚失败');
                                                                                                                               }
                                                                                                                               done();
                                                                                                                               callback(err, null);
                                                                                                                           });
                                                                                                                       } else {
                                                                                                                           done();
                                                                                                                           return callback(err, result);//删除成功
                                                                                                                       }
                                                                                                                   });
                                                                                                               }
                                                                                                           }
                                                                                                       })
                                                                                                   })

                                                                                               }
                                                                                               if (wlen == 0) {
                                                                                                   client.query(sqlCommit, function (errCommit) {
                                                                                                       if (errCommit) {
                                                                                                           client.query(sqlRollback, function (errRollback) {
                                                                                                               if (errRollback) {
                                                                                                                   log.error('回滚失败');
                                                                                                               }
                                                                                                               done();
                                                                                                               callback(err, null);
                                                                                                           });
                                                                                                       } else {
                                                                                                           done();
                                                                                                           return callback(err, result);//删除成功
                                                                                                       }
                                                                                                   });
                                                                                               }
                                                                                           }
                                                                                       })
                                                                                   }else {
                                                                                       client.query(sqlCommit, function (errCommit) {
                                                                                           if (errCommit) {
                                                                                               client.query(sqlRollback, function (errRollback) {
                                                                                                   if (errRollback) {
                                                                                                       log.error('回滚失败');
                                                                                                   }
                                                                                                   done();
                                                                                                   callback(err, null);
                                                                                               });
                                                                                           } else {
                                                                                               done();
                                                                                               return callback(err, result);//删除成功
                                                                                           }
                                                                                       });
                                                                                   }

                                                                               }
                                                                           })
                                                                       }

                                                                   }
                                                               })

                                                           }
                                                       }
                                                   })
                                               }
                                           })
                                       }
                                   })
                               }
                           })
                       }
                   });
                });
            });
        }
    });
};
/**
 * app根据分类查照片别表
 * @param params
 * @param callback
 */
exports.picture_type = function(params, args, callback){
    let date1 =  new Date().getTime();
    let date2 = new Date(date1 - 1000 * 60 * 60 * 6);//6小时之内
    let app_list_sql1 = 'SELECT id, name, imgurl, "desc", want, own, point, username, author, pushtime  FROM picture WHERE 1=1';
    let number = 1;
    const p_sql4 = 'SELECT * FROM wantlogs WHERE userid = $1 AND type = $2 AND pid = $3';
    const p_sql5 = 'SELECT * FROM ownlogs WHERE userid = $1 AND type = $2 AND pid = $3';
    const p_sql6 = 'SELECT * FROM type WHERE id = $1 AND status = $2';
    if(args.type&&args.type != 'all'){
        app_list_sql1 += ' AND tid = $'+number;//app图片列表
        number++;
    }
    if(args.userid){
        app_list_sql1 +=' AND author = $'+number;
        number++;
    }
    if(args.pid){
        app_list_sql1 +=' AND id != $'+number;
        number++;
    }
    if(args.notice == "1"){
        app_list_sql1 +=' AND pushtime > $'+number;
        number++;
    }
    app_list_sql1 += ' AND status = $'+(number+2)+' AND del = $'+(number+3)+' ORDER BY recommend ASC, pushtime DESC LIMIT $'+number+' OFFSET $'+(number+1);//app图片列表
    const sql = 'SELECT w.createtime, w.userid, u.headurl FROM wantlogs w, users u WHERE w.userid=u.id AND w.pid = $1 AND w.createtime > $2 AND w.type = $3 ORDER BY w.createtime DESC LIMIT 1 OFFSET 0';//查找想要记录中最后一次想要时间

    //var last_time;
    //var headurl;
    var info;
    let infos = [];
    if(args.type&&args.type != 'all'){
        pg.query(p_sql6, [args.type, 1], function(err, result){
            if(err){
                callback (err, null);
            }else if(result.rowCount == 0) {
                callback (err, 'null_type');
            }else {
                pg.query(app_list_sql1, params, function(err, result){//执行查找

                    if(err){
                        callback (err, null);//报错
                    }else {
                        let pictures = result.rows;
                        let len = pictures.length;
                        let a = 0;
                        if(len > 0 ){
                            pg.connect(function (err, client, done) { //获取数据库链接
                                client.query(sqlStart, function () { //开启事务
                                    async.mapSeries(pictures,function (picture,callback) {
                                        let paramswant = [
                                            picture.id,
                                            date2,
                                            1
                                        ];
                                        const params4 = [
                                            args.myid,
                                            1,
                                            picture.id
                                        ]
                                        client.query(sql, paramswant, function (err, result) {//想要记录
                                            if (err) {
                                                callback(err, result);
                                            } else if (result.rowCount == 0) {
                                                let last_time = null;
                                                let headurl = null;
                                                client.query(p_sql4, params4, function (err, result) {//想要人数
                                                    if (err) {
                                                        callback(err, result);
                                                    } else {
                                                        let u_want = 0;
                                                        u_want = result.rowCount;
                                                        client.query(p_sql5, params4, function (err, result) {//拥有人数
                                                            if (err) {
                                                                callback(err, result);
                                                            } else {
                                                                let u_own = result.rowCount;
                                                                info = {
                                                                    id: picture.id,
                                                                    name: picture.name,
                                                                    imgurl: picture.imgurl,
                                                                    desc: picture.desc,
                                                                    want: picture.want,
                                                                    own: picture.own,
                                                                    point: picture.point,
                                                                    author: picture.author,
                                                                    pushtime: picture.pushtime,
                                                                    username: picture.username,
                                                                    headurl: headurl,
                                                                    last_time: last_time,
                                                                    u_want: u_want,
                                                                    u_own: u_own
                                                                };
                                                                a++;
                                                                infos.push(info);
                                                                callback(err, info);//结果

                                                                ;
                                                            }
                                                        })
                                                    }
                                                })
                                            } else {
                                                let last_time = result.rows[0].createtime;
                                                let headurl = result.rows[0].headurl;
                                                client.query(p_sql4, params4, function (err, result) {//想要人数
                                                    if (err) {
                                                        callback(err, result);
                                                    } else {
                                                        let u_want = 0;
                                                        u_want = result.rowCount;
                                                        client.query(p_sql5, params4, function (err, result) {//拥有人数
                                                            if (err) {
                                                                callback(err, result);
                                                            } else {
                                                                let u_own = result.rowCount;
                                                                info = {
                                                                    id: picture.id,
                                                                    name: picture.name,
                                                                    imgurl: picture.imgurl,
                                                                    desc: picture.desc,
                                                                    want: picture.want,
                                                                    own: picture.own,
                                                                    point: picture.point,
                                                                    author: picture.author,
                                                                    pushtime: picture.pushtime,
                                                                    username: picture.username,
                                                                    headurl: headurl,
                                                                    last_time: last_time,
                                                                    u_want: u_want,
                                                                    u_own: u_own
                                                                };
                                                                a++;
                                                                infos.push(info);
                                                                callback(err, info);//结果

                                                                ;
                                                            }
                                                        })
                                                    }
                                                })
                                            }
                                        });
                                        },
                                        function (err, n) { //循环完毕提交事务
                                            if (err) {
                                                log.error(err);
                                                log.error('回滚.............');
                                                client.query(sqlRollback, function (errRollback) {
                                                    if (errRollback) {
                                                        log.error('回滚失败');
                                                        log.error(errRollback);
                                                    }
                                                    done();
                                                    callback(errRollback, null);
                                                });
                                            } else {
                                                log.debug('提交.............');
                                                client.query(sqlCommit, function (errCommit) {
                                                    if (errCommit) {
                                                        client.query(sqlRollback, function (errRollback) {
                                                            if (errRollback) {
                                                                console.log('回滚失败');
                                                                log.error(errRollback);
                                                            }
                                                            done(); //关闭链接
                                                            callback(errCommit, infos);
                                                        });
                                                    } else {
                                                        done();
                                                        callback(errCommit, infos);
                                                    }

                                                });
                                            }
                                        })
                                })
                            })

                        }else {
                            callback (err, infos);//没作品
                        }

                    }
                });
            }
        })
    }else {
        pg.query(app_list_sql1, params, function(err, result){//执行查找

            if(err){
                callback (err, null);//报错
            }else {
                let pictures = result.rows;
                let len = pictures.length;
                let a = 0;
                if(len > 0 ){
                    pg.connect(function (err, client, done) { //获取数据库链接
                        client.query(sqlStart, function () { //开启事务
                            async.mapSeries(pictures, function (picture, callback) {
                                let paramswant = [
                                    picture.id,
                                    date2,
                                    1
                                ];
                                const params4 = [
                                    args.myid,
                                    1,
                                    picture.id
                                ]
                                pg.query(sql, paramswant, function (err, result) {
                                    if (err) {
                                        callback(err, result);
                                    } else if (result.rowCount == 0) {
                                        let last_time = null;
                                        let headurl = null;
                                        pg.query(p_sql4, params4, function (err, result) {
                                            if (err) {
                                                callback(err, result);
                                            } else {
                                                let u_want = 0;
                                                u_want = result.rowCount;
                                                pg.query(p_sql5, params4, function (err, result) {
                                                    if (err) {
                                                        callback(err, result);
                                                    } else {
                                                        let u_own = result.rowCount;
                                                        info = {
                                                            id: picture.id,
                                                            name: picture.name,
                                                            imgurl: picture.imgurl,
                                                            desc: picture.desc,
                                                            want: picture.want,
                                                            own: picture.own,
                                                            point: picture.point,
                                                            author: picture.author,
                                                            pushtime: picture.pushtime,
                                                            username: picture.username,
                                                            headurl: headurl,
                                                            last_time: last_time,
                                                            u_want: u_want,
                                                            u_own: u_own
                                                        };
                                                        a++;
                                                        infos.push(info);
                                                            callback(err, info);//结果

                                                        ;
                                                    }
                                                })
                                            }
                                        })
                                    } else {
                                        let last_time = result.rows[0].createtime;
                                        let headurl = result.rows[0].headurl;
                                        pg.query(p_sql4, params4, function (err, result) {
                                            if (err) {
                                                callback(err, result);
                                            } else {
                                                let u_want = 0;
                                                u_want = result.rowCount;
                                                pg.query(p_sql5, params4, function (err, result) {
                                                    if (err) {
                                                        callback(err, result);
                                                    } else {
                                                        let u_own = result.rowCount;
                                                        info = {
                                                            id: picture.id,
                                                            name: picture.name,
                                                            imgurl: picture.imgurl,
                                                            desc: picture.desc,
                                                            want: picture.want,
                                                            own: picture.own,
                                                            point: picture.point,
                                                            author: picture.author,
                                                            pushtime: picture.pushtime,
                                                            username: picture.username,
                                                            headurl: headurl,
                                                            last_time: last_time,
                                                            u_want: u_want,
                                                            u_own: u_own
                                                        };
                                                        a++;
                                                        infos.push(info);
                                                            callback(err, info);//结果

                                                        ;
                                                    }
                                                })
                                            }
                                        })
                                    }

                                });
                            },
                                function (err, n) { //循环完毕提交事务
                                    if (err) {
                                        log.error(err);
                                        log.error('回滚.............');
                                        client.query(sqlRollback, function (errRollback) {
                                            if (errRollback) {
                                                log.error('回滚失败');
                                                log.error(errRollback);
                                            }
                                            done();
                                            callback(errRollback, null);
                                        });
                                    } else {
                                        log.debug('提交.............');
                                        client.query(sqlCommit, function (errCommit) {
                                            if (errCommit) {
                                                client.query(sqlRollback, function (errRollback) {
                                                    if (errRollback) {
                                                        console.log('回滚失败');
                                                        log.error(errRollback);
                                                    }
                                                    done(); //关闭链接
                                                    callback(errCommit, infos);
                                                });
                                            } else {
                                                done();
                                                callback(errCommit, infos);
                                            }

                                        });
                                    }
                                })

                        })
                    })

                }else {
                    callback (err, infos);//没作品
                }

            }
        });
    }

};
// /**
//  * app根据分类查照片别表
//  * @param params
//  * @param callback
//  */
// exports.picture_type = function(params, args, callback){
//     var app_list_sql1;
//     if(args.type == 'all'){
//         app_list_sql1 = 'SELECT id, name, imgurl, "desc", want, own FROM picture WHERE status = $3 AND del = $4 ORDER BY recommend ASC, pushtime DESC LIMIT $1 OFFSET $2';//app图片列表
//     }else {
//         app_list_sql1 = 'SELECT id, name, imgurl, "desc", want, own FROM picture WHERE tid = $1 AND status = $4 AND del = $5 ORDER BY recommend ASC, pushtime DESC LIMIT $2 OFFSET $3';//app图片列表
//
//     }
//     pg.query(app_list_sql1, params, function(err, result){//执行查找
//         if(err){
//             callback (err, null);//报错
//         }else {
//             callback (err, result);//结果
//         }
//     });
// };
/**
 * app根据分类查照片别表数据统计
 * @param params
 * @param callback
 */
exports.picture_type_count = function(params, args, callback){
    var app_count_sql1;
    if(args.type == 'all'){
        app_count_sql1 = 'SELECT count(*) FROM picture WHERE tatus = $1 AND del = $2';//app图片列表
    }else {
        app_count_sql1 = 'SELECT count(*) FROM picture WHERE tid = $1 AND status = $2 AND del = $3';//app图片列表
    }
    pg.query(app_count_sql1, params, function(err, result){//执行查找
        if(err){
            callback (err, null);//报错
        }else {
            callback (err, result);//结果
        }
    });
};
/**
 * app用户想要图片
 * @param params
 * @param args
 * @param callback
 */
const want_sql1 = 'SELECT * FROM picture WHERE id = $1 AND del = $2';//查图片
const want_sql2 = 'SELECT * FROM wantlogs WHERE pid = $1 AND userid = $2 AND type = $3';//查是否已经添加想要
const want_sql3 = 'INSERT INTO wantlogs (pid, userid, type, createtime) VALUES ($1, $2, $3, $4)';//添加想要记录
const want_sql4 = 'SELECT * FROM users WHERE id = $1';//查用户想要作品数
const want_sql5 = 'UPDATE users SET  p_wantnum = $1 WHERE id = $2';//修改用户想要作品数
const want_sql6 = 'UPDATE picture SET  want = $1 WHERE id = $2';//修改作品想要数
exports.want = function(params, args, req, callback){
    const params1 = [
        args.pictureid,
        1
    ];
    const params2 = [
        args.pictureid,
        req.user.id,
        1
    ];
    const params4 = [
        req.user.id
    ];
    pg.query(want_sql1, params1, function(err, result){//执行查照片操作
        if(err){
            callback (err, null);//报错
        }else if(result.rowCount == 0){
            callback (err, 'null_picture');//没有这个图片
        }else {
            let picture = result.rows[0];
            const params6 = [
                picture.want>0?Number(picture.want)+1:1,//照片想要人数
                picture.id//照片id
            ];
            pg.query(want_sql2, params2, function(err, result){//执行查想要记录
                if(err){
                    callback (err, null);//报错
                }else if(result.rowCount > 0){
                    callback (err, 'exit_logs');//该用户已经对这个图片添加想要
                }else {
                    pg.connect(function (err, client, done) {
                        client.query(sqlStart, function () {
                            client.query(want_sql3, params, function (err, result) {//添加想要记录
                                if (err || result.rowCount == 0) {
                                    log.error('添加想要记录失败');
                                    client.query(sqlRollback, function (errRollback) {
                                        log.error('回滚');
                                        if (errRollback) {
                                            log.error('回滚失败');
                                        }
                                        done();
                                        callback(err, null);
                                    })
                                } else {
                                    client.query(want_sql6, params6, function(err, result){//修改照片想要人数失败
                                        if(err||result.rowCount == 0){
                                            log.error('修改照片想要人数失败');
                                            client.query(sqlRollback, function(errRollback){
                                                log.error('回滚');
                                                if(errRollback){
                                                    log.error('回滚失败');
                                                }
                                                done();
                                                callback(err, null);
                                            })
                                        }else{
                                            client.query(want_sql4, params4, function(err, result){//查想要人的信息
                                                if(err||result.rowCount == 0){
                                                    log.error('查想要人的信息失败');
                                                    client.query(sqlRollback, function(errRollback){
                                                        log.error('回滚');
                                                        if(errRollback){
                                                            log.error('回滚失败');
                                                        }
                                                        done();
                                                        callback(err, null);
                                                    })
                                                }else{
                                                    let user = result.rows[0];
                                                    const params5 = [
                                                        user.p_wantnum>0?Number(user.p_wantnum)+1:1,//数量
                                                        user.id//用户id
                                                    ];
                                                    client.query(want_sql5, params5, function(err, result){//修改用户想要作品数
                                                        if(err||result.rowCount == 0){
                                                            log.error('修改用户想要作品数失败');
                                                            client.query(sqlRollback, function(errRollback){
                                                                log.error('回滚');
                                                                if(errRollback){
                                                                    log.error('回滚失败');
                                                                }
                                                                done();
                                                                callback(err, null);
                                                            })
                                                        }else{
                                                            client.query(want_sql1, params1, function(err, result){//修改用户想要作品数
                                                                if(err||result.rowCount == 0){
                                                                    log.error('修改用户想要作品数失败');
                                                                    client.query(sqlRollback, function(errRollback){
                                                                        log.error('回滚');
                                                                        if(errRollback){
                                                                            log.error('回滚失败');
                                                                        }
                                                                        done();
                                                                        callback(err, null);
                                                                    })
                                                                }else{
                                                                    let info = result.rows[0].want;
                                                                    client.query(sqlCommit, function (errCommit) {//结束进程
                                                                        if (errCommit) {
                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                if (errRollback) {
                                                                                    log.error('回滚失败');
                                                                                }
                                                                                done();
                                                                                callback(err, null);
                                                                            });
                                                                        } else {
                                                                            done();
                                                                            return callback(err, info);//添加想要成功
                                                                        }
                                                                    });
                                                                }
                                                            })
                                                        }
                                                    })

                                                }
                                            })
                                        }
                                    })
                                }
                            })
                        })
                    })
                }
            });
        }
    });
};
/**
 * app用户拥有图片
 * @param params
 * @param args
 * @param callback
 */
const own_sql1 = 'SELECT * FROM picture WHERE id = $1 AND del = $2';//查图片
const own_sql2 = 'SELECT * FROM ownlogs WHERE pid = $1 AND userid = $2 AND type = $3';//查是否已经添加拥有
const own_sql3 = 'INSERT INTO ownlogs (pid, userid, type, createtime) VALUES ($1, $2, $3, $4)';//添加拥有记录
const own_sql4 = 'SELECT * FROM users WHERE id = $1';//查用户拥有作品数
const own_sql5 = 'UPDATE users SET  p_ownnum = $1 WHERE id = $2';//修改用户拥有作品数
const own_sql6 = 'UPDATE picture SET  own = $1 WHERE id = $2';//修改作品拥有数
exports.own = function(params, args, req, callback){
    const params1 = [
        args.pictureid,
        1
    ];
    const params2 = [
        args.pictureid,
        req.user.id,
        1
    ];
    const params4 = [
        req.user.id
    ];
    pg.query(own_sql1, params1, function(err, result){//执行查照片操作
        if(err){
            callback (err, null);//报错
        }else if(result.rowCount == 0){
            callback (err, 'null_picture');//没有这个图片
        }else {
            let picture = result.rows[0];
            pg.query(own_sql2, params2, function(err, result){//执行查拥有记录
                if(err){
                    callback (err, null);//报错
                }else if(result.rowCount > 0){
                    callback (err, 'exit_logs');//该用户已经对这个图片添加拥有
                }else {
                    pg.connect(function (err, client, done) {
                        client.query(sqlStart, function () {
                            client.query(own_sql3, params, function (err, result) {//添加拥有记录
                                if (err || result.rowCount == 0) {
                                    log.error('添加拥有记录失败');
                                    client.query(sqlRollback, function (errRollback) {
                                        log.error('回滚');
                                        if (errRollback) {
                                            log.error('回滚失败');
                                        }
                                        done();
                                        callback(err, null);
                                    })
                                } else {
                                    const params6 = [
                                        picture.own>0?Number(picture.own)+1:1,//照片想要人数
                                        picture.id//照片id
                                    ];
                                    client.query(own_sql6, params6, function(err, result){//修改照片拥有人数失败
                                        if(err||result.rowCount == 0){
                                            log.error('修改照片拥有人数失败');
                                            client.query(sqlRollback, function(errRollback){
                                                log.error('回滚');
                                                if(errRollback){
                                                    log.error('回滚失败');
                                                }
                                                done();
                                                callback(err, null);
                                            })
                                        }else{
                                            client.query(own_sql4, params4, function(err, result){//查拥有人的信息
                                                if(err||result.rowCount == 0){
                                                    log.error('查拥有人的信息失败');
                                                    client.query(sqlRollback, function(errRollback){
                                                        log.error('回滚');
                                                        if(errRollback){
                                                            log.error('回滚失败');
                                                        }
                                                        done();
                                                        callback(err, null);
                                                    })
                                                }else{
                                                    let user = result.rows[0];
                                                    const params5 = [
                                                        user.p_ownnum>0?Number(user.p_ownnum)+1:1,//数量
                                                        user.id//用户id
                                                    ];
                                                    client.query(own_sql5, params5, function(err, result){//修改用户拥有作品数
                                                        if(err||result.rowCount == 0){
                                                            log.error('修改用户拥有作品数失败');
                                                            client.query(sqlRollback, function(errRollback){
                                                                log.error('回滚');
                                                                if(errRollback){
                                                                    log.error('回滚失败');
                                                                }
                                                                done();
                                                                callback(err, null);
                                                            })
                                                        }else{
                                                            client.query(own_sql1, params1, function(err, result){//修改用户拥有作品数
                                                                if(err||result.rowCount == 0){
                                                                    log.error('修改用户拥有作品数失败');
                                                                    client.query(sqlRollback, function(errRollback){
                                                                        log.error('回滚');
                                                                        if(errRollback){
                                                                            log.error('回滚失败');
                                                                        }
                                                                        done();
                                                                        callback(err, null);
                                                                    })
                                                                }else{
                                                                    let info = result.rows[0].own;
                                                                    client.query(sqlCommit, function (errCommit) {//结束进程
                                                                        if (errCommit) {
                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                if (errRollback) {
                                                                                    log.error('回滚失败');
                                                                                }
                                                                                done();
                                                                                callback(err, null);
                                                                            });
                                                                        } else {
                                                                            done();
                                                                            return callback(err, info);//添加拥有成功
                                                                        }
                                                                    });
                                                                }
                                                            })
                                                        }
                                                    })
                                                }
                                            })
                                        }
                                    })
                                }
                            })
                        })
                    })
                }
            });
        }
    });
};
/**
 * app用户想要图片
 * @param params
 * @param args
 * @param callback
 */
const unwant_sql1 = 'SELECT * FROM picture WHERE id = $1 AND del = $2';//查图片
const unwant_sql2 = 'SELECT * FROM wantlogs WHERE pid = $1 AND userid = $2 AND type = $3';//查是否已经添加想要
const unwant_sql3 = 'DELETE FROM wantlogs WHERE pid = $1 AND userid = $2 AND type = $3';//添加想要记录
const unwant_sql4 = 'SELECT * FROM users WHERE id = $1';//查用户想要作品数
const unwant_sql5 = 'UPDATE users SET  p_wantnum = $1 WHERE id = $2';//修改用户想要作品数
const unwant_sql6 = 'UPDATE picture SET  want = $1 WHERE id = $2';//修改作品想要数
exports.unwant = function(params, args, req, callback){
    const params1 = [
        args.pictureid,
        1
    ];
    const params2 = [
        args.pictureid,
        req.user.id,
        1
    ];
    const params4 = [
        req.user.id
    ];
    pg.query(unwant_sql1, params1, function(err, result){//执行查照片操作
        if(err){
            callback (err, null);//报错
        }else if(result.rowCount == 0){
            callback (err, 'null_picture');//没有这个图片
        }else {
            let picture = result.rows[0];
            const params6 = [
                picture.want>1?Number(picture.want)-1:0,//照片想要人数
                picture.id//照片id
            ];
            pg.query(unwant_sql2, params2, function(err, result){//执行查想要记录
                if(err){
                    callback (err, null);//报错
                }else if(result.rowCount == 0){
                    callback (err, 'null_logs');//该用户没有对这个图片添加想要
                }else {
                    pg.connect(function (err, client, done) {
                        client.query(sqlStart, function () {
                            client.query(unwant_sql3, params, function (err, result) {//添加想要记录
                                if (err || result.rowCount == 0) {
                                    log.error('添加想要记录失败');
                                    client.query(sqlRollback, function (errRollback) {
                                        log.error('回滚');
                                        if (errRollback) {
                                            log.error('回滚失败');
                                        }
                                        done();
                                        callback(err, null);
                                    })
                                } else {
                                    client.query(unwant_sql6, params6, function(err, result){//修改照片想要人数失败
                                        if(err||result.rowCount == 0){
                                            log.error('修改照片想要人数失败');
                                            client.query(sqlRollback, function(errRollback){
                                                log.error('回滚');
                                                if(errRollback){
                                                    log.error('回滚失败');
                                                }
                                                done();
                                                callback(err, null);
                                            })
                                        }else{
                                            client.query(unwant_sql4, params4, function(err, result){//查想要人的信息
                                                if(err||result.rowCount == 0){
                                                    log.error('查想要人的信息失败');
                                                    client.query(sqlRollback, function(errRollback){
                                                        log.error('回滚');
                                                        if(errRollback){
                                                            log.error('回滚失败');
                                                        }
                                                        done();
                                                        callback(err, null);
                                                    })
                                                }else{
                                                    let user = result.rows[0];
                                                    const params5 = [
                                                        user.p_wantnum>1?Number(user.p_wantnum)-1:0,//数量
                                                        user.id//用户id
                                                    ];
                                                    client.query(unwant_sql5, params5, function(err, result){//修改用户想要作品数
                                                        if(err||result.rowCount == 0){
                                                            log.error('修改用户想要作品数失败');
                                                            client.query(sqlRollback, function(errRollback){
                                                                log.error('回滚');
                                                                if(errRollback){
                                                                    log.error('回滚失败');
                                                                }
                                                                done();
                                                                callback(err, null);
                                                            })
                                                        }else{
                                                            client.query(unwant_sql1, params1, function(err, result){//修改用户想要作品数
                                                                if(err||result.rowCount == 0){
                                                                    log.error('修改用户想要作品数失败');
                                                                    client.query(sqlRollback, function(errRollback){
                                                                        log.error('回滚');
                                                                        if(errRollback){
                                                                            log.error('回滚失败');
                                                                        }
                                                                        done();
                                                                        callback(err, null);
                                                                    })
                                                                }else{
                                                                    let info = result.rows[0].want;
                                                                    client.query(sqlCommit, function (errCommit) {//结束进程
                                                                        if (errCommit) {
                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                if (errRollback) {
                                                                                    log.error('回滚失败');
                                                                                }
                                                                                done();
                                                                                callback(err, null);
                                                                            });
                                                                        } else {
                                                                            done();
                                                                            return callback(err, info);//添加想要成功
                                                                        }
                                                                    });
                                                                }
                                                            })
                                                        }
                                                    })

                                                }
                                            })
                                        }
                                    })
                                }
                            })
                        })
                    })
                }
            });
        }
    });
};
/**
 * app用户拥有图片
 * @param params
 * @param args
 * @param callback
 */
const unown_sql1 = 'SELECT * FROM picture WHERE id = $1 AND del = $2';//查图片
const unown_sql2 = 'SELECT * FROM ownlogs WHERE pid = $1 AND userid = $2 AND type = $3';//查是否已经添加拥有
const unown_sql3 = 'DELETE FROM ownlogs WHERE pid = $1 AND userid = $2 AND type = $3';//添加拥有记录
const unown_sql4 = 'SELECT * FROM users WHERE id = $1';//查用户拥有作品数
const unown_sql5 = 'UPDATE users SET  p_ownnum = $1 WHERE id = $2';//修改用户拥有作品数
const unown_sql6 = 'UPDATE picture SET  own = $1 WHERE id = $2';//修改作品拥有数
exports.unown = function(params, args, req, callback){
    const params1 = [
        args.pictureid,
        1
    ];
    const params2 = [
        args.pictureid,
        req.user.id,
        1
    ];
    const params4 = [
        req.user.id
    ];
    pg.query(unown_sql1, params1, function(err, result){//执行查照片操作
        if(err){
            callback (err, null);//报错
        }else if(result.rowCount == 0){
            callback (err, 'null_picture');//没有这个图片
        }else {
            let picture = result.rows[0];
            pg.query(unown_sql2, params2, function(err, result){//执行查拥有记录
                if(err){
                    callback (err, null);//报错
                }else if(result.rowCount == 0){
                    callback (err, 'null_logs');//该用户已经对这个图片添加拥有
                }else {
                    pg.connect(function (err, client, done) {
                        client.query(sqlStart, function () {
                            client.query(unown_sql3, params, function (err, result) {//添加拥有记录
                                if (err || result.rowCount == 0) {
                                    log.error('添加拥有记录失败');
                                    client.query(sqlRollback, function (errRollback) {
                                        log.error('回滚');
                                        if (errRollback) {
                                            log.error('回滚失败');
                                        }
                                        done();
                                        callback(err, null);
                                    })
                                } else {
                                    const params6 = [
                                        picture.own>1?Number(picture.own)-1:0,//照片想要人数
                                        picture.id//照片id
                                    ];
                                    client.query(unown_sql6, params6, function(err, result){//修改照片拥有人数失败
                                        if(err||result.rowCount == 0){
                                            log.error('修改照片拥有人数失败');
                                            client.query(sqlRollback, function(errRollback){
                                                log.error('回滚');
                                                if(errRollback){
                                                    log.error('回滚失败');
                                                }
                                                done();
                                                callback(err, null);
                                            })
                                        }else{
                                            client.query(unown_sql4, params4, function(err, result){//查拥有人的信息
                                                if(err||result.rowCount == 0){
                                                    log.error('查拥有人的信息失败');
                                                    client.query(sqlRollback, function(errRollback){
                                                        log.error('回滚');
                                                        if(errRollback){
                                                            log.error('回滚失败');
                                                        }
                                                        done();
                                                        callback(err, null);
                                                    })
                                                }else{
                                                    let user = result.rows[0];
                                                    const params5 = [
                                                        user.p_ownnum>0?Number(user.p_ownnum)-1:0,//数量
                                                        user.id//用户id
                                                    ];
                                                    client.query(unown_sql5, params5, function(err, result){//修改用户拥有作品数
                                                        if(err||result.rowCount == 0){
                                                            log.error('修改用户拥有作品数失败');
                                                            client.query(sqlRollback, function(errRollback){
                                                                log.error('回滚');
                                                                if(errRollback){
                                                                    log.error('回滚失败');
                                                                }
                                                                done();
                                                                callback(err, null);
                                                            })
                                                        }else{
                                                            client.query(unown_sql1, params1, function(err, result){//修改用户拥有作品数
                                                                if(err||result.rowCount == 0){
                                                                    log.error('修改用户拥有作品数失败');
                                                                    client.query(sqlRollback, function(errRollback){
                                                                        log.error('回滚');
                                                                        if(errRollback){
                                                                            log.error('回滚失败');
                                                                        }
                                                                        done();
                                                                        callback(err, null);
                                                                    })
                                                                }else{
                                                                    let info = result.rows[0].own;
                                                                    client.query(sqlCommit, function (errCommit) {//结束进程
                                                                        if (errCommit) {
                                                                            client.query(sqlRollback, function (errRollback) {
                                                                                if (errRollback) {
                                                                                    log.error('回滚失败');
                                                                                }
                                                                                done();
                                                                                callback(err, null);
                                                                            });
                                                                        } else {
                                                                            done();
                                                                            return callback(err, info);//添加拥有成功
                                                                        }
                                                                    });
                                                                }
                                                            })
                                                        }
                                                    })
                                                }
                                            })
                                        }
                                    })
                                }
                            })
                        })
                    })
                }
            });
        }
    });
};
// /**
//  * app 用户追加的作品
//  * @param params
//  * @param callback
//  */
// const user_sql = 'SELECT * FROM users WHERE id = $1';
// const add_list_sql = 'SELECT imgurl FROM picture WHERE author = $1 AND status = $2 AND del = $3 ORDER BY pushtime DESC LIMIT $4 OFFSET $5';//查询用户发布的作品
// exports.add_list = function(params, callback){
//     const u_params = [
//         params[0]
//     ];
//     pg.query(user_sql, u_params, function(err, result){//执行查询操作
//         if(err){
//             callback (err, null);//报错
//         }else if(result.rowCount == 0){
//             callback (err, 'null_user');//没有用户
//         }else {
//             pg.query(add_list_sql, params, function(err, result){//执行查询操作
//                 if(err){
//                     callback (err, null);//报错
//                 }else {
//                     callback (err, result);//返回数据
//                 }
//             });
//         }
//     });
//
// };
/**
 * app 用户追加的作品
 * @param params
 * @param callback
 */
exports.add_list = function(params, args, callback){
    let date1 =  new Date().getTime();
    let date2 = new Date(date1 - 1000 * 60 * 60 * 6);//6小时之内
    let app_list_sql1 = 'SELECT id, name, imgurl, "desc", want, own, point, username, author, pushtime  FROM picture WHERE author = $1 AND status = $2 AND del = $3 ORDER BY pushtime DESC LIMIT $4 OFFSET $5';
    const p_sql4 = 'SELECT * FROM wantlogs WHERE userid = $1 AND type = $2 AND pid = $3';
    const p_sql5 = 'SELECT * FROM ownlogs WHERE userid = $1 AND type = $2 AND pid = $3';
    const sql = 'SELECT w.createtime, w.userid, u.headurl FROM wantlogs w, users u WHERE w.userid=u.id AND w.pid = $1 AND w.createtime > $2 AND w.type = $3 ORDER BY w.createtime DESC LIMIT 1 OFFSET 0';//查找想要记录中最后一次想要时间
    var info;
    let infos = [];

            pg.query(app_list_sql1, params, function(err, result){//执行查找

                if(err){
                    callback (err, null);//报错
                }else {
                    let pictures = result.rows;
                    let len = pictures.length;
                    let a = 0;
                    if(len > 0 ){
                        pg.connect(function (err, client, done) { //获取数据库链接
                            client.query(sqlStart, function () { //开启事务
                                async.mapSeries(pictures,function (picture,callback) {
                                        let paramswant = [
                                            picture.id,
                                            date2,
                                            1
                                        ];
                                        const params4 = [
                                            args.userid,
                                            1,
                                            picture.id
                                        ]
                                        client.query(sql, paramswant, function (err, result) {//想要记录
                                            if (err) {
                                                callback(err, result);
                                            } else if (result.rowCount == 0) {
                                                let last_time = null;
                                                let headurl = null;
                                                client.query(p_sql4, params4, function (err, result) {//想要人数
                                                    if (err) {
                                                        callback(err, result);
                                                    } else {
                                                        let u_want = 0;
                                                        u_want = result.rowCount;
                                                        client.query(p_sql5, params4, function (err, result) {//拥有人数
                                                            if (err) {
                                                                callback(err, result);
                                                            } else {
                                                                let u_own = result.rowCount;
                                                                info = {
                                                                    id: picture.id,
                                                                    name: picture.name,
                                                                    imgurl: picture.imgurl,
                                                                    desc: picture.desc,
                                                                    want: picture.want,
                                                                    own: picture.own,
                                                                    point: picture.point,
                                                                    author: picture.author,
                                                                    pushtime: picture.pushtime,
                                                                    username: picture.username,
                                                                    headurl: headurl,
                                                                    last_time: last_time,
                                                                    u_want: u_want,
                                                                    u_own: u_own
                                                                };
                                                                a++;
                                                                infos.push(info);
                                                                callback(err, info);//结果

                                                                ;
                                                            }
                                                        })
                                                    }
                                                })
                                            } else {
                                                let last_time = result.rows[0].createtime;
                                                let headurl = result.rows[0].headurl;
                                                client.query(p_sql4, params4, function (err, result) {//想要人数
                                                    if (err) {
                                                        callback(err, result);
                                                    } else {
                                                        let u_want = 0;
                                                        u_want = result.rowCount;
                                                        client.query(p_sql5, params4, function (err, result) {//拥有人数
                                                            if (err) {
                                                                callback(err, result);
                                                            } else {
                                                                let u_own = result.rowCount;
                                                                info = {
                                                                    id: picture.id,
                                                                    name: picture.name,
                                                                    imgurl: picture.imgurl,
                                                                    desc: picture.desc,
                                                                    want: picture.want,
                                                                    own: picture.own,
                                                                    point: picture.point,
                                                                    author: picture.author,
                                                                    pushtime: picture.pushtime,
                                                                    username: picture.username,
                                                                    headurl: headurl,
                                                                    last_time: last_time,
                                                                    u_want: u_want,
                                                                    u_own: u_own
                                                                };
                                                                a++;
                                                                infos.push(info);
                                                                callback(err, info);//结果

                                                                ;
                                                            }
                                                        })
                                                    }
                                                })
                                            }
                                        });
                                    },
                                    function (err, n) { //循环完毕提交事务
                                        if (err) {
                                            log.error(err);
                                            log.error('回滚.............');
                                            client.query(sqlRollback, function (errRollback) {
                                                if (errRollback) {
                                                    log.error('回滚失败');
                                                    log.error(errRollback);
                                                }
                                                done();
                                                callback(errRollback, null);
                                            });
                                        } else {
                                            log.debug('提交.............');
                                            client.query(sqlCommit, function (errCommit) {
                                                if (errCommit) {
                                                    client.query(sqlRollback, function (errRollback) {
                                                        if (errRollback) {
                                                            console.log('回滚失败');
                                                            log.error(errRollback);
                                                        }
                                                        done(); //关闭链接
                                                        callback(errCommit, infos);
                                                    });
                                                } else {
                                                    done();
                                                    callback(errCommit, infos);
                                                }

                                            });
                                        }
                                    })
                            })
                        })

                    }else {
                        callback (err, infos);//没作品
                    }

                }
            });

};
/**
 *
 * @param params
 * @param callback
 */
const user_sql2 = 'SELECT * FROM users WHERE id = $1';
const want_list_sql = 'SELECT pid FROM wantlogs WHERE userid = $1 AND type = $2 ORDER BY createtime DESC';//查用户想要的作品记录（只有图片作品）
exports.want_list = function(params, params1, args, callback){
    let date1 =  new Date().getTime();
    let date2 = new Date(date1 - 1000 * 60 * 60 * 6);//6小时之内
    const p_sql4 = 'SELECT * FROM wantlogs WHERE userid = $1 AND type = $2 AND pid = $3';
    const p_sql5 = 'SELECT * FROM ownlogs WHERE userid = $1 AND type = $2 AND pid = $3';
    const sql = 'SELECT w.createtime, w.userid, u.headurl FROM wantlogs w, users u WHERE w.userid=u.id AND w.pid = $1 AND w.createtime > $2 AND w.type = $3 ORDER BY w.createtime DESC LIMIT 1 OFFSET 0';//查找想要记录中最后一次想要时间
    var info;
    let infos = [];
    let theids=[];
    const u_params = [
        params1[0]
    ];
    pg.query(user_sql2, u_params, function(err, result) {//执行查询操作
        if (err) {
            callback(err, null);//报错
        } else if (result.rowCount == 0) {
            callback(err, 'null_user');//没有用户
        } else {
            pg.query(want_list_sql, params1, function (err, result) {//执行查询想要记录
                if (err) {
                    callback(err, null);//报错
                } else {
                    let results = result.rows;
                    results.forEach(function (one_result) {
                        let len = results.length;
                        if (len == 0) {
                            theids.push(one_result.pid);
                        } else {
                            let a = theids.indexOf(one_result.pid);
                            if (a == -1) {
                                theids.push(one_result.pid);
                            }
                        }
                    });
                    log.debug(theids);
                    if (theids.length > 0) {
                        theids = "'" + theids.join("','") + "'";//将数组ids整理成字符串
                        //const sql = 'SELECT imgurl FROM picture WHERE id IN (' + theids + ') AND status = $1 AND del = $2 ORDER BY pushtime DESC LIMIT $3 OFFSET $4';////查找照片
                        let app_list_sql1 = 'SELECT id, name, imgurl, "desc", want, own, point, username, author, pushtime  FROM picture WHERE id IN (' + theids + ') AND status = $1 AND del = $2 ORDER BY pushtime DESC LIMIT $3 OFFSET $4';
                        pg.query(app_list_sql1, params, function(err, result){//执行查找
                            if(err){
                                callback (err, null);//报错
                            }else {
                                let pictures = result.rows;
                                let len = pictures.length;
                                let a = 0;
                                if(len > 0 ){
                                    pg.connect(function (err, client, done) { //获取数据库链接
                                        client.query(sqlStart, function () { //开启事务
                                            async.mapSeries(pictures,function (picture,callback) {
                                                    let paramswant = [
                                                        picture.id,
                                                        date2,
                                                        1
                                                    ];
                                                    const params4 = [
                                                        args.userid,
                                                        1,
                                                        picture.id
                                                    ]
                                                    client.query(sql, paramswant, function (err, result) {//想要记录
                                                        if (err) {
                                                            callback(err, result);
                                                        } else if (result.rowCount == 0) {
                                                            let last_time = null;
                                                            let headurl = null;
                                                            client.query(p_sql4, params4, function (err, result) {//想要人数
                                                                if (err) {
                                                                    callback(err, result);
                                                                } else {
                                                                    let u_want = 0;
                                                                    u_want = result.rowCount;
                                                                    client.query(p_sql5, params4, function (err, result) {//拥有人数
                                                                        if (err) {
                                                                            callback(err, result);
                                                                        } else {
                                                                            let u_own = result.rowCount;
                                                                            info = {
                                                                                id: picture.id,
                                                                                name: picture.name,
                                                                                imgurl: picture.imgurl,
                                                                                desc: picture.desc,
                                                                                want: picture.want,
                                                                                own: picture.own,
                                                                                point: picture.point,
                                                                                author: picture.author,
                                                                                pushtime: picture.pushtime,
                                                                                username: picture.username,
                                                                                headurl: headurl,
                                                                                last_time: last_time,
                                                                                u_want: u_want,
                                                                                u_own: u_own
                                                                            };
                                                                            a++;
                                                                            infos.push(info);
                                                                            callback(err, info);//结果

                                                                            ;
                                                                        }
                                                                    })
                                                                }
                                                            })
                                                        } else {
                                                            let last_time = result.rows[0].createtime;
                                                            let headurl = result.rows[0].headurl;
                                                            client.query(p_sql4, params4, function (err, result) {//想要人数
                                                                if (err) {
                                                                    callback(err, result);
                                                                } else {
                                                                    let u_want = 0;
                                                                    u_want = result.rowCount;
                                                                    client.query(p_sql5, params4, function (err, result) {//拥有人数
                                                                        if (err) {
                                                                            callback(err, result);
                                                                        } else {
                                                                            let u_own = result.rowCount;
                                                                            info = {
                                                                                id: picture.id,
                                                                                name: picture.name,
                                                                                imgurl: picture.imgurl,
                                                                                desc: picture.desc,
                                                                                want: picture.want,
                                                                                own: picture.own,
                                                                                point: picture.point,
                                                                                author: picture.author,
                                                                                pushtime: picture.pushtime,
                                                                                username: picture.username,
                                                                                headurl: headurl,
                                                                                last_time: last_time,
                                                                                u_want: u_want,
                                                                                u_own: u_own
                                                                            };
                                                                            a++;
                                                                            infos.push(info);
                                                                            callback(err, info);//结果

                                                                            ;
                                                                        }
                                                                    })
                                                                }
                                                            })
                                                        }
                                                    });
                                                },
                                                function (err, n) { //循环完毕提交事务
                                                    if (err) {
                                                        log.error(err);
                                                        log.error('回滚.............');
                                                        client.query(sqlRollback, function (errRollback) {
                                                            if (errRollback) {
                                                                log.error('回滚失败');
                                                                log.error(errRollback);
                                                            }
                                                            done();
                                                            callback(errRollback, null);
                                                        });
                                                    } else {
                                                        log.debug('提交.............');
                                                        client.query(sqlCommit, function (errCommit) {
                                                            if (errCommit) {
                                                                client.query(sqlRollback, function (errRollback) {
                                                                    if (errRollback) {
                                                                        console.log('回滚失败');
                                                                        log.error(errRollback);
                                                                    }
                                                                    done(); //关闭链接
                                                                    callback(errCommit, infos);
                                                                });
                                                            } else {
                                                                done();
                                                                callback(errCommit, infos);
                                                            }

                                                        });
                                                    }
                                                })
                                        })
                                    })

                                }else {
                                    callback (err, infos);//没作品
                                }

                            }
                        });
                    } else {
                        callback(err, 'null');
                    }
                }
            });
        }
    })
};


// /**
//  *
//  * @param params
//  * @param callback
//  */
// const user_sql2 = 'SELECT * FROM users WHERE id = $1';
// const want_list_sql = 'SELECT pid FROM wantlogs WHERE userid = $1 AND type = $2';//查用户想要的作品记录（只有图片作品）
// exports.want_list = function(params, params1, callback){
//     let theids=[];
//     const u_params = [
//         params1[0]
//     ];
//     pg.query(user_sql2, u_params, function(err, result) {//执行查询操作
//         if (err) {
//             callback(err, null);//报错
//         } else if (result.rowCount == 0) {
//             callback(err, 'null_user');//没有用户
//         } else {
//             pg.query(want_list_sql, params1, function (err, result) {//执行查询想要记录
//                 if (err) {
//                     callback(err, null);//报错
//                 } else {
//                     let results = result.rows;
//                     results.forEach(function (one_result) {
//                         let len = results.length;
//                         if (len == 0) {
//                             theids.push(one_result.pid);
//                         } else {
//                             let a = theids.indexOf(one_result.pid);
//                             if (a == -1) {
//                                 theids.push(one_result.pid);
//                             }
//                         }
//                     });
//                     log.debug(theids);
//                     if (theids.length > 0) {
//                         theids = "'" + theids.join("','") + "'";//将数组ids整理成字符串
//                         const sql = 'SELECT imgurl FROM picture WHERE id IN (' + theids + ') AND status = $1 AND del = $2 ORDER BY pushtime DESC LIMIT $3 OFFSET $4';////查找照片
//                         pg.query(sql, params, function (err, result) {//执行操作
//                             if (err) {
//                                 callback(err, null);//报错
//                             } else {
//                                 callback(err, result);//返回数据
//                             }
//                         })
//                     } else {
//                         callback(err, 'null');
//                     }
//                 }
//             });
//         }
//     })
// };
/**
 *
 * @param params
 * @param callback
 */
const user_sql3 = 'SELECT * FROM users WHERE id = $1';
const own_list_sql = 'SELECT pid FROM ownlogs WHERE userid = $1 AND type = $2 ORDER BY createtime DESC';//查用户拥有的作品记录（只有图片作品）
exports.own_list = function(params, params1, args, callback){
    let date1 =  new Date().getTime();
    let date2 = new Date(date1 - 1000 * 60 * 60 * 6);//6小时之内
    const p_sql4 = 'SELECT * FROM wantlogs WHERE userid = $1 AND type = $2 AND pid = $3';
    const p_sql5 = 'SELECT * FROM ownlogs WHERE userid = $1 AND type = $2 AND pid = $3';
    const sql = 'SELECT w.createtime, w.userid, u.headurl FROM wantlogs w, users u WHERE w.userid=u.id AND w.pid = $1 AND w.createtime > $2 AND w.type = $3 ORDER BY w.createtime DESC LIMIT 1 OFFSET 0';//查找想要记录中最后一次想要时间
    var info;
    let infos = [];
    let theids=[];
    const u_params = [
        params1[0]
    ];
    pg.query(user_sql3, u_params, function(err, result) {//执行查询操作
        if (err) {
            callback(err, null);//报错
        } else if (result.rowCount == 0) {
            callback(err, 'null_user');//没有用户
        } else {
            pg.query(own_list_sql, params1, function (err, result) {//执行查询想要记录
                if (err) {
                    callback(err, null);//报错
                } else {
                    let results = result.rows;
                    results.forEach(function (one_result) {
                        let len = results.length;
                        if (len == 0) {
                            theids.push(one_result.pid);
                        } else {
                            let a = theids.indexOf(one_result.pid);
                            if (a == -1) {
                                theids.push(one_result.pid);
                            }
                        }
                    });
                    log.debug(theids);
                    if (theids.length > 0) {
                        theids = "'" + theids.join("','") + "'";//将数组ids整理成字符串
                        //const sql = 'SELECT imgurl FROM picture WHERE id IN (' + theids + ') AND status = $1 AND del = $2 ORDER BY pushtime DESC LIMIT $3 OFFSET $4';////查找照片
                        let app_list_sql1 = 'SELECT id, name, imgurl, "desc", want, own, point, username, author, pushtime  FROM picture WHERE id IN (' + theids + ') AND status = $1 AND del = $2 ORDER BY pushtime DESC LIMIT $3 OFFSET $4';
                        pg.query(app_list_sql1, params, function(err, result){//执行查找
                            if(err){
                                callback (err, null);//报错
                            }else {
                                let pictures = result.rows;
                                let len = pictures.length;
                                let a = 0;
                                if(len > 0 ){
                                    pg.connect(function (err, client, done) { //获取数据库链接
                                        client.query(sqlStart, function () { //开启事务
                                            async.mapSeries(pictures,function (picture,callback) {
                                                    let paramswant = [
                                                        picture.id,
                                                        date2,
                                                        1
                                                    ];
                                                    const params4 = [
                                                        args.userid,
                                                        1,
                                                        picture.id
                                                    ]
                                                    client.query(sql, paramswant, function (err, result) {//想要记录
                                                        if (err) {
                                                            callback(err, result);
                                                        } else if (result.rowCount == 0) {
                                                            let last_time = null;
                                                            let headurl = null;
                                                            client.query(p_sql4, params4, function (err, result) {//想要人数
                                                                if (err) {
                                                                    callback(err, result);
                                                                } else {
                                                                    let u_want = 0;
                                                                    u_want = result.rowCount;
                                                                    client.query(p_sql5, params4, function (err, result) {//拥有人数
                                                                        if (err) {
                                                                            callback(err, result);
                                                                        } else {
                                                                            let u_own = result.rowCount;
                                                                            info = {
                                                                                id: picture.id,
                                                                                name: picture.name,
                                                                                imgurl: picture.imgurl,
                                                                                desc: picture.desc,
                                                                                want: picture.want,
                                                                                own: picture.own,
                                                                                point: picture.point,
                                                                                author: picture.author,
                                                                                pushtime: picture.pushtime,
                                                                                username: picture.username,
                                                                                headurl: headurl,
                                                                                last_time: last_time,
                                                                                u_want: u_want,
                                                                                u_own: u_own
                                                                            };
                                                                            a++;
                                                                            infos.push(info);
                                                                            callback(err, info);//结果

                                                                            ;
                                                                        }
                                                                    })
                                                                }
                                                            })
                                                        } else {
                                                            let last_time = result.rows[0].createtime;
                                                            let headurl = result.rows[0].headurl;
                                                            client.query(p_sql4, params4, function (err, result) {//想要人数
                                                                if (err) {
                                                                    callback(err, result);
                                                                } else {
                                                                    let u_want = 0;
                                                                    u_want = result.rowCount;
                                                                    client.query(p_sql5, params4, function (err, result) {//拥有人数
                                                                        if (err) {
                                                                            callback(err, result);
                                                                        } else {
                                                                            let u_own = result.rowCount;
                                                                            info = {
                                                                                id: picture.id,
                                                                                name: picture.name,
                                                                                imgurl: picture.imgurl,
                                                                                desc: picture.desc,
                                                                                want: picture.want,
                                                                                own: picture.own,
                                                                                point: picture.point,
                                                                                author: picture.author,
                                                                                pushtime: picture.pushtime,
                                                                                username: picture.username,
                                                                                headurl: headurl,
                                                                                last_time: last_time,
                                                                                u_want: u_want,
                                                                                u_own: u_own
                                                                            };
                                                                            a++;
                                                                            infos.push(info);
                                                                            callback(err, info);//结果

                                                                            ;
                                                                        }
                                                                    })
                                                                }
                                                            })
                                                        }
                                                    });
                                                },
                                                function (err, n) { //循环完毕提交事务
                                                    if (err) {
                                                        log.error(err);
                                                        log.error('回滚.............');
                                                        client.query(sqlRollback, function (errRollback) {
                                                            if (errRollback) {
                                                                log.error('回滚失败');
                                                                log.error(errRollback);
                                                            }
                                                            done();
                                                            callback(errRollback, null);
                                                        });
                                                    } else {
                                                        log.debug('提交.............');
                                                        client.query(sqlCommit, function (errCommit) {
                                                            if (errCommit) {
                                                                client.query(sqlRollback, function (errRollback) {
                                                                    if (errRollback) {
                                                                        console.log('回滚失败');
                                                                        log.error(errRollback);
                                                                    }
                                                                    done(); //关闭链接
                                                                    callback(errCommit, infos);
                                                                });
                                                            } else {
                                                                done();
                                                                callback(errCommit, infos);
                                                            }

                                                        });
                                                    }
                                                })
                                        })
                                    })

                                }else {
                                    callback (err, infos);//没作品
                                }

                            }
                        });
                    } else {
                        callback(err, 'null');
                    }
                }
            });
        }
    })
};
// /**
//  *
//  * @param params
//  * @param callback
//  */
// const user_sql3 = 'SELECT * FROM users WHERE id = $1';
// const own_list_sql = 'SELECT pid FROM ownlogs WHERE userid = $1 AND type = $2';//查用户拥有的作品记录（只有图片作品）
// exports.own_list = function(params, params1, callback){
//     let theids=[];
//     const u_params = [
//         params1[0]
//     ];
//     pg.query(user_sql3, u_params, function(err, result) {//执行查询操作
//         if (err) {
//             callback(err, null);//报错
//         } else if (result.rowCount == 0) {
//             callback(err, 'null_user');//没有用户
//         } else {
//             pg.query(own_list_sql, params1, function (err, result) {//执行查询想要记录
//                 if (err) {
//                     callback(err, null);//报错
//                 } else {
//                     let results = result.rows;
//                     results.forEach(function (one_result) {
//                         let len = results.length;
//                         if (len == 0) {
//                             theids.push(one_result.pid);
//                         } else {
//                             let a = theids.indexOf(one_result.pid);
//                             if (a == -1) {
//                                 theids.push(one_result.pid);
//                             }
//                         }
//                     });
//                     log.debug(theids);
//                     if (theids.length > 0) {
//                         theids = "'" + theids.join("','") + "'";//将数组ids整理成字符串
//                         const sql = 'SELECT imgurl FROM picture WHERE id IN (' + theids + ') AND status = $1 AND del = $2 ORDER BY pushtime DESC LIMIT $3 OFFSET $4';////查找照片
//                         pg.query(sql, params, function (err, result) {//执行操作
//                             if (err) {
//                                 callback(err, null);//报错
//                             } else {
//                                 callback(err, result);//返回数据
//                             }
//                         })
//                     } else {
//                         callback(err, 'null');
//                     }
//                 }
//             });
//         }
//     })
// };
// /**
//  * 设置图片首页推荐
//  * @param args
//  * @param callback
//  */
// exports.recomment = function(args, callback){
//     let ids = args.ids;//ids 是数组
//     let theids = "'"+ids.join("','")+"'";//将数组ids整理成字符串
//     const rec_sql1 = 'SELECT * FROM picture WHERE id IN ('+theids+') AND del = $1';//查找id在theids的图片数据
//     const rec_sql2 = 'SELECT * FROM recommend WHERE pid IN ('+theids+')';//查是否又已经被推荐的作品
//     const rec_sql3 = 'INSERT INTO recommend (pid, createtime) VALUES ($1, $2)';//添加推荐
//     const rec_sql4 = 'UPDATE picture SET recommend = $1 WHERE  id IN ('+theids+')';//添加推荐
//     const params1 = [2];//删除的作品
//     pg.query(rec_sql1, params1, function(err, result){//查找是否有对已经删除的作品进行推荐
//         if(err){
//             callback (err, null);//报错
//         }else if(result.rowCount > 0){
//             callback (err, 'exit_null');//含有已经被删除的作品
//         }else{
//             pg.query(rec_sql2, [], function(err, result){//查找是否含有已经备推荐的作品
//                 if(err){
//                     callback (err, null);//报错
//                 }else if(result.rowCount > 0){
//                     callback (err, 'exit_rec');//含有已经被推荐的作品
//                 }else {
//                     pg.connect(function (err, client, done) {
//                         client.query(sqlStart, function () {
//                             client.query(rec_sql4, [1], function (err, result) {//修改图片推荐
//                                 if (err || result.rowCount == 0) {
//                                     log.error('编辑图片推荐状态失败');
//                                     client.query(sqlRollback, function (errRollback) {
//                                         log.error('回滚失败');
//                                         if (errRollback) {
//                                             log.error('回滚失败')
//                                         }
//                                         done();
//                                         callback(err, null);
//                                     })
//                                 } else {
//                                     let len = ids.length;
//                                     let a = 1;
//                                     ids.forEach(function (id) {
//                                         const params3 = [
//                                             id,
//                                             new Date()
//                                         ];
//                                         pg.query(rec_sql3, params3, function (err, result) {//执行添加推荐操作
//                                             if (err || result.rowCount == 0) {
//                                                 log.error('添加推荐失败');
//                                                 client.query(sqlRollback, function (errRollback) {
//                                                     log.error('回滚失败');
//                                                     if (errRollback) {
//                                                         log.error('回滚失败')
//                                                     }
//                                                     done();
//                                                     callback(err, null);
//                                                 })
//                                             } else {
//                                                 if (a == len) {
//                                                     callback(err, result);//推荐完成
//                                                 } else {
//                                                     a++;
//                                                 }
//                                             }
//                                         })
//                                     })
//                                 }
//                             })
//                         })
//                     })
//                 }
//             })
//         }
//     })
// };
/**
 * 推荐的作品
 * @param params
 * @param req
 * @param callback
 */
const others_sql1 = 'SELECT tid FROM picture WHERE id = $1';//查这张照片的分类
const others_sql2 = 'SELECT id, name, imgurl, "desc", want, own, point, username, author, pushtime  FROM picture WHERE tid = $1 AND status = $2 AND del = $3 AND id != $4 ORDER BY want DESC, own DESC, pushtime DESC LIMIT 10 OFFSET 0';
const others_sql3 = 'SELECT * FROM wantlogs WHERE userid = $1 AND type = $2 AND pid = $3';
const others_sql4 = 'SELECT * FROM ownlogs WHERE userid = $1 AND type = $2 AND pid = $3';
exports.others = function(params, req, callback){
    let date1 =  new Date().getTime();
    let date2 = new Date(date1 - 1000 * 60 * 60 * 6);//6小时之内
    const sql = 'SELECT w.createtime, w.userid, u.headurl FROM wantlogs w, users u WHERE w.userid=u.id AND w.pid = $1 AND w.createtime > $2 AND w.type = $3 ORDER BY w.createtime DESC LIMIT 1 OFFSET 0';//查找想要记录中最后一次想要时间
    //var last_time;
    //var headurl;
    var info;
    let infos = [];
    pg.query(others_sql1, params, function(err, result){
        if(err){
            callback (err, null);
        }else if(result.rowCount == 0){
            callback (err, null);
        }else {
            let params2 = [
                result.rows[0].tid,
                1,
                1,
                params[0]
            ];
            pg.query(others_sql2, params2, function(err, result){//执行查找
                if(err){
                    callback (err, null);//报错
                }else {
                    let pictures = result.rows;
                    let len = pictures.length;
                    let a = 0;
                    if(len > 0 ){
                        pg.connect(function (err, client, done) { //获取数据库链接
                            client.query(sqlStart, function () { //开启事务
                                async.mapSeries(pictures, function (picture, callback) {
                                        let paramswant = [
                                            picture.id,
                                            date2,
                                            1
                                        ];
                                        const params4 = [
                                            req.user.id,
                                            1,
                                            picture.id
                                        ]
                                        pg.query(sql, paramswant, function (err, result) {
                                            if (err) {
                                                callback(err, result);
                                            } else if (result.rowCount == 0) {
                                                let last_time = null;
                                                let headurl = null;
                                                pg.query(others_sql3, params4, function (err, result) {
                                                    if (err) {
                                                        callback(err, result);
                                                    } else {
                                                        let u_want = 0;
                                                        u_want = result.rowCount;
                                                        pg.query(others_sql4, params4, function (err, result) {
                                                            if (err) {
                                                                callback(err, result);
                                                            } else {
                                                                let u_own = result.rowCount;
                                                                info = {
                                                                    id: picture.id,
                                                                    name: picture.name,
                                                                    imgurl: picture.imgurl,
                                                                    desc: picture.desc,
                                                                    want: picture.want,
                                                                    own: picture.own,
                                                                    point: picture.point,
                                                                    author: picture.author,
                                                                    pushtime: picture.pushtime,
                                                                    username: picture.username,
                                                                    headurl: headurl,
                                                                    last_time: last_time,
                                                                    u_want: u_want,
                                                                    u_own: u_own
                                                                };
                                                                a++;
                                                                infos.push(info);
                                                                callback(err, info);//结果

                                                                ;
                                                            }
                                                        })
                                                    }
                                                })
                                            } else {
                                                let last_time = result.rows[0].createtime;
                                                let headurl = result.rows[0].headurl;
                                                pg.query(others_sql3, params4, function (err, result) {
                                                    if (err) {
                                                        callback(err, result);
                                                    } else {
                                                        let u_want = 0;
                                                        u_want = result.rowCount;
                                                        pg.query(others_sql4, params4, function (err, result) {
                                                            if (err) {
                                                                callback(err, result);
                                                            } else {
                                                                let u_own = result.rowCount;
                                                                info = {
                                                                    id: picture.id,
                                                                    name: picture.name,
                                                                    imgurl: picture.imgurl,
                                                                    desc: picture.desc,
                                                                    want: picture.want,
                                                                    own: picture.own,
                                                                    point: picture.point,
                                                                    author: picture.author,
                                                                    pushtime: picture.pushtime,
                                                                    username: picture.username,
                                                                    headurl: headurl,
                                                                    last_time: last_time,
                                                                    u_want: u_want,
                                                                    u_own: u_own
                                                                };
                                                                a++;
                                                                infos.push(info);
                                                                callback(err, info);//结果

                                                                ;
                                                            }
                                                        })
                                                    }
                                                })
                                            }

                                        });
                                    },
                                    function (err, n) { //循环完毕提交事务
                                        if (err) {
                                            log.error(err);
                                            log.error('回滚.............');
                                            client.query(sqlRollback, function (errRollback) {
                                                if (errRollback) {
                                                    log.error('回滚失败');
                                                    log.error(errRollback);
                                                }
                                                done();
                                                callback(errRollback, null);
                                            });
                                        } else {
                                            log.debug('提交.............');
                                            client.query(sqlCommit, function (errCommit) {
                                                if (errCommit) {
                                                    client.query(sqlRollback, function (errRollback) {
                                                        if (errRollback) {
                                                            console.log('回滚失败');
                                                            log.error(errRollback);
                                                        }
                                                        done(); //关闭链接
                                                        callback(errCommit, infos);
                                                    });
                                                } else {
                                                    done();
                                                    callback(errCommit, infos);
                                                }

                                            });
                                        }
                                    })

                            })
                        })

                    }else {
                        callback (err, infos);//没作品
                    }

                }
            });
        }
    })

}